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