codesnippets:listcomprehension
This is an old revision of the document!
List comprehension
- syntactic sugar to apply set operations to lists
- syntax := “[” output expression “|” variable / binding = input set “,” predicate “]”
- e.g.
[ 1 / x | x ← [-1,-0.75..1], x /= 0 ]- creates a list
- of reciprocal numbers (
1 / x) - from the set {-1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1} (
[-1,-0.75..1]) - except 0 (
x /= 0)
-
- excutes, with output:
[-1.0,-1.3333333333333333,-2.0,-4.0,4.0,2.0,1.3333333333333333,1.0]
- binding can be applied to pattern matching to filter out sums
You could leave a comment if you were logged in.
codesnippets/listcomprehension.1618055275.txt.gz · Last modified: (external edit)
