codesnippets:listcomprehension
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.txt · Last modified: by 127.0.0.1
