5 A list with a constant length can be written by closing the list
\r
6 elements in brackets and separating the elements by commas:
\r
11 ## List comprehension
\r
13 Lists can be formed with list comprehension expressions that
\r
14 resemble set comprehension in mathematics. For example
\r
18 creates a list of all elements in `lst` increased by one
\r
21 [x+y | x <- lstX, y <- lstY]
\r
23 creates list of all sums of pairs where one element is in `lstX` and one in `lstY`.
\r
24 Note that the list may contain duplicates.
\r
26 It is possible to add also constraints
\r
28 [x `div` 2 | x <- lst, x `mod` 2 == 0]
\r
32 [x*y | x <- lst, y = x+1]
\r
34 Formally, a list comprehension expression is
\r
36 [<expression> | <list qualifier>, ..., <list qualifier>]
\r
38 where list qualifier can be one of the following
\r
40 * generator `<variable> <- <list expression>`
\r
42 * definition `<variable> = <expression>`
\r
43 * `then <expression> by <expression>`
\r
45 The last type of qualifier can be used to make operations that affects the
\r
46 whole list, for example
\r
50 removes the first three elements and
\r
54 sorts the elements by `x`.
\r
56 ## Accessing the list elements
\r
58 The most basic way of reading list is to
\r
59 read it element by element.
\r
62 ::value[Prelude/length]
\r
66 Lists like almost all other types support the generic equality and comparison operations:
\r
68 ::value[Prelude/==,Prelude/!=]
\r
69 ::value[Prelude/<,Prelude/<=,Prelude/>,Prelude/>=]
\r
71 For example `lst == []` tests whether the `lst` is empty.
\r
72 The comparison of the list is lexicographic.
\r
74 ## Executing code for each list element
\r
76 The only difference between the following iteration functions is
\r
77 the order of their parameters:
\r
79 ::value[Prelude/iter, Prelude/for]
\r
81 ## Concatenating lists
\r
83 ::value[Prelude/+, Prelude/sum]
\r
85 ## List transformations
\r
87 ::value[Prelude/map]
\r
88 ::value[Prelude/filter]
\r
89 ::value[Prelude/join]
\r
90 ::value[Prelude/concatMap]
\r
91 ::value[Prelude/mapMaybe]
\r
93 ::value[Prelude/zip]
\r
94 ::value[Prelude/zipWith]
\r
95 ::value[Prelude/unzip]
\r
99 The following functions modify the order of the list elements:
\r
101 ::value[Prelude/sort, Prelude/sortBy, Prelude/sortWith]
\r
102 ::value[Prelude/reverse]
\r
106 The following functions extract some sublist of the given list:
\r
108 ::value[Prelude/take]
\r
109 ::value[Prelude/drop]
\r
110 ::value[Prelude/sub]
\r
112 ## Aggregate operations
\r
114 Sometimes it is necessary to compute some kind of summary over
\r
115 all elements of a list. The most useful generic aggregate operation is `foldl`.
\r
117 ::value[Prelude/foldl]
\r
119 It is used to define many more specialized aggreate operations such as
\r
122 product = foldl (*) 1
\r
123 maximum = foldl1 max
\r
125 There is a variant that traverses the list from right to left:
\r
127 ::value[Prelude/foldr]
\r
129 and a variant that that assumes that the list has at least one element:
\r
131 ::value[Prelude/foldl1]