]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.tutorial/scl/Tutorial/1.05 Lists.md
Merge "Fix to OrderedSetTemplate, preference for autocompletion"
[simantics/platform.git] / bundles / org.simantics.scl.tutorial / scl / Tutorial / 1.05 Lists.md
1 # Lists\r
2 \r
3 ## List literals\r
4 \r
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
7 \r
8     [1,2,3]\r
9     []\r
10 \r
11 ## List comprehension\r
12 \r
13 Lists can be formed with list comprehension expressions that\r
14 resemble set comprehension in mathematics. For example\r
15 \r
16     [x+1 | x <- lst]\r
17 \r
18 creates a list of all elements in `lst` increased by one\r
19 and \r
20 \r
21     [x+y | x <- lstX, y <- lstY]\r
22 \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
25 \r
26 It is possible to add also constraints \r
27 \r
28     [x `div` 2 | x <- lst, x `mod` 2 == 0]\r
29 \r
30 and definitions\r
31 \r
32     [x*y | x <- lst, y = x+1] \r
33 \r
34 Formally, a list comprehension expression is\r
35 \r
36     [<expression> | <list qualifier>, ..., <list qualifier>]\r
37     \r
38 where list qualifier can be one of the following\r
39 \r
40 * generator `<variable> <- <list expression>`\r
41 * guard `<boolean>`\r
42 * definition `<variable> = <expression>`\r
43 * `then <expression> by <expression>`\r
44 \r
45 The last type of qualifier can be used to make operations that affects the\r
46 whole list, for example\r
47 \r
48     then drop 3\r
49     \r
50 removes the first three elements and\r
51 \r
52     then sortBy by x\r
53     \r
54 sorts the elements by `x`.\r
55 \r
56 ## Accessing the list elements\r
57 \r
58 The most basic way of reading list is to\r
59 read it element by element.\r
60 \r
61 ::value[Prelude/!]\r
62 ::value[Prelude/length]\r
63 \r
64 ## Comparison\r
65 \r
66 Lists like almost all other types support the generic equality and comparison operations:\r
67 \r
68 ::value[Prelude/==,Prelude/!=]\r
69 ::value[Prelude/<,Prelude/<=,Prelude/>,Prelude/>=]\r
70 \r
71 For example `lst == []` tests whether the `lst` is empty.\r
72 The comparison of the list is lexicographic.\r
73 \r
74 ## Executing code for each list element\r
75 \r
76 The only difference between the following iteration functions is\r
77 the order of their parameters:\r
78 \r
79 ::value[Prelude/iter, Prelude/for]\r
80 \r
81 ## Concatenating lists\r
82 \r
83 ::value[Prelude/+, Prelude/sum]\r
84 \r
85 ## List transformations\r
86 \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
92 \r
93 ::value[Prelude/zip]\r
94 ::value[Prelude/zipWith]\r
95 ::value[Prelude/unzip]\r
96 \r
97 ## Ordering\r
98 \r
99 The following functions modify the order of the list elements:\r
100 \r
101 ::value[Prelude/sort, Prelude/sortBy, Prelude/sortWith]\r
102 ::value[Prelude/reverse]\r
103 \r
104 ## Sublists\r
105 \r
106 The following functions extract some sublist of the given list:\r
107 \r
108 ::value[Prelude/take]\r
109 ::value[Prelude/drop]\r
110 ::value[Prelude/sub]\r
111 \r
112 ## Aggregate operations\r
113 \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
116 \r
117 ::value[Prelude/foldl]\r
118 \r
119 It is used to define many more specialized aggreate operations such as\r
120 \r
121     sum     = foldl (+) 0\r
122     product = foldl (*) 1\r
123     maximum = foldl1 max\r
124 \r
125 There is a variant that traverses the list from right to left: \r
126 \r
127 ::value[Prelude/foldr]\r
128 \r
129 and a variant that that assumes that the list has at least one element:\r
130 \r
131 ::value[Prelude/foldl1]