APIList

List

Immutable lists and their basic traversals.

The prelude imports the List type, so annotations need no import, and grants qualified access, as in List.map; import List exposing (map) brings a function into scope unqualified. Lists are written with brackets, which need no import at all.

List

type List a    = Nil    | Cons a (List a)Source

An immutable sequence of values of type a. Source code builds and matches lists with brackets: [], [1, 2, 3], and [head | tail] for the list with head in front of tail. Adding to the front and taking the head or tail are constant time, and tails are shared rather than copied.

Lists compare element by element, a proper prefix ordered first, and display in bracket syntax.

[1 | [2, 3]] == [1, 2, 3]
[1, 2] < [1, 3]
[1] < [1, 0]
show [1, 2, 3] == "[1, 2, 3]"

Constructors

Nil

Nil : List aSource

The empty list, written [].

Cons

Cons : a -> List a -> List aSource

An element in front of a list, written [head | tail]. Prefer the bracket syntax to spelling the constructor out.

Instances:

Decode a => Decode (List a)
Encode a => Encode (List a)
Eq a => Eq (List a)
Ord a => Ord (List a)
Show a => Show (List a)

each

each : (a ->{e} ()) -> List a ->{e} ()Source

Calls action on every element, first to last, for its effects.

import State

total = State.run 0 { List.each { n -> State.put (State.get () + n) } [1, 2, 3] }

total.state == 6

filter

filter : (a ->{e} Bool) -> List a ->{e} List aSource

The elements for which keep answers True, in their original order. keep runs once per element, first to last. It runs in constant stack.

List.filter { n -> n > 1 } [3, 1, 2] == [3, 2]

foldl

foldl : (a -> b ->{e} b) -> b -> List a ->{e} bSource

Combines the elements into one value, first to last: combine receives each element and the result so far, starting from result.

List.foldl { n total -> total + n } 0 [1, 2, 3] == 6
List.foldl { n acc -> [n | acc] } [] [1, 2, 3] == [3, 2, 1]

foldr

foldr : (a -> b ->{e} b) -> b -> List a ->{e} bSource

Combines the elements into one value, last to first: combine receives each element and the result of combining the elements after it. It runs in constant stack, and the callback’s effects happen in that same last-to-first order.

List.foldr { n acc -> [n | acc] } [] [1, 2, 3] == [1, 2, 3]
List.foldr { s acc -> s ++ acc } "" ["a", "b", "c"] == "abc"

length

length : List a -> IntSource

The number of elements. It traverses the whole list.

List.length [1, 2, 3] == 3
List.length [] == 0

map

map : (a ->{e} b) -> List a ->{e} List bSource

Applies fn to every element, first to last, keeping their order. It runs in constant stack.

List.map { n -> n * 10 } [1, 2, 3] == [10, 20, 30]

range

range : (Num a, Ord a) => a -> a -> List aSource

The values from start through end, counting up by one, or [] when start is greater than end. It works at both Int and Float; the bounds must be finite, since construction is not guaranteed to end for NaN or an infinity. Range.each visits the same values without building a list.

List.range 1 4 == [1, 2, 3, 4]
List.range 3 1 == []
List.range 0.5 2.0 == [0.5, 1.5]

reverse

reverse : List a -> List aSource

The same elements in the opposite order.

List.reverse [1, 2, 3] == [3, 2, 1]