]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.tutorial/scl/Tutorial/1.05 Lists.md
Import org.simantics.scl.tutorial from incubator SVN repo
[simantics/platform.git] / bundles / org.simantics.scl.tutorial / scl / Tutorial / 1.05 Lists.md
diff --git a/bundles/org.simantics.scl.tutorial/scl/Tutorial/1.05 Lists.md b/bundles/org.simantics.scl.tutorial/scl/Tutorial/1.05 Lists.md
new file mode 100644 (file)
index 0000000..a7a4063
--- /dev/null
@@ -0,0 +1,131 @@
+# Lists\r
+\r
+## List literals\r
+\r
+A list with a constant length can be written by closing the list\r
+elements in brackets and separating the elements by commas:\r
+\r
+    [1,2,3]\r
+    []\r
+\r
+## List comprehension\r
+\r
+Lists can be formed with list comprehension expressions that\r
+resemble set comprehension in mathematics. For example\r
+\r
+    [x+1 | x <- lst]\r
+\r
+creates a list of all elements in `lst` increased by one\r
+and \r
+\r
+    [x+y | x <- lstX, y <- lstY]\r
+\r
+creates list of all sums of pairs where one element is in `lstX` and one in `lstY`.\r
+Note that the list may contain duplicates.\r
+\r
+It is possible to add also constraints \r
+\r
+    [x `div` 2 | x <- lst, x `mod` 2 == 0]\r
+\r
+and definitions\r
+\r
+    [x*y | x <- lst, y = x+1] \r
+\r
+Formally, a list comprehension expression is\r
+\r
+    [<expression> | <list qualifier>, ..., <list qualifier>]\r
+    \r
+where list qualifier can be one of the following\r
+\r
+* generator `<variable> <- <list expression>`\r
+* guard `<boolean>`\r
+* definition `<variable> = <expression>`\r
+* `then <expression> by <expression>`\r
+\r
+The last type of qualifier can be used to make operations that affects the\r
+whole list, for example\r
+\r
+    then drop 3\r
+    \r
+removes the first three elements and\r
+\r
+    then sortBy by x\r
+    \r
+sorts the elements by `x`.\r
+\r
+## Accessing the list elements\r
+\r
+The most basic way of reading list is to\r
+read it element by element.\r
+\r
+::value[Prelude/!]\r
+::value[Prelude/length]\r
+\r
+## Comparison\r
+\r
+Lists like almost all other types support the generic equality and comparison operations:\r
+\r
+::value[Prelude/==,Prelude/!=]\r
+::value[Prelude/<,Prelude/<=,Prelude/>,Prelude/>=]\r
+\r
+For example `lst == []` tests whether the `lst` is empty.\r
+The comparison of the list is lexicographic.\r
+\r
+## Executing code for each list element\r
+\r
+The only difference between the following iteration functions is\r
+the order of their parameters:\r
+\r
+::value[Prelude/iter, Prelude/for]\r
+\r
+## Concatenating lists\r
+\r
+::value[Prelude/+, Prelude/sum]\r
+\r
+## List transformations\r
+\r
+::value[Prelude/map]\r
+::value[Prelude/filter]\r
+::value[Prelude/join]\r
+::value[Prelude/concatMap]\r
+::value[Prelude/mapMaybe]\r
+\r
+::value[Prelude/zip]\r
+::value[Prelude/zipWith]\r
+::value[Prelude/unzip]\r
+\r
+## Ordering\r
+\r
+The following functions modify the order of the list elements:\r
+\r
+::value[Prelude/sort, Prelude/sortBy, Prelude/sortWith]\r
+::value[Prelude/reverse]\r
+\r
+## Sublists\r
+\r
+The following functions extract some sublist of the given list:\r
+\r
+::value[Prelude/take]\r
+::value[Prelude/drop]\r
+::value[Prelude/sub]\r
+\r
+## Aggregate operations\r
+\r
+Sometimes it is necessary to compute some kind of summary over\r
+all elements of a list. The most useful generic aggregate operation is `foldl`.\r
+\r
+::value[Prelude/foldl]\r
+\r
+It is used to define many more specialized aggreate operations such as\r
+\r
+    sum     = foldl (+) 0\r
+    product = foldl (*) 1\r
+    maximum = foldl1 max\r
+\r
+There is a variant that traverses the list from right to left: \r
+\r
+::value[Prelude/foldr]\r
+\r
+and a variant that that assumes that the list has at least one element:\r
+\r
+::value[Prelude/foldl1] 
\ No newline at end of file