====== 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'')
* code:
main :: IO ()
main = print x
x = [ 1 / x | x <- [-1,-0.75..1], 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
* example:
import Prelude hiding (Maybe(..))
main :: IO ()
main = print x
data Maybe a = Just a | Nothing
x = [ch | (Just ch) <- [Just '3', Nothing, Just '4']]
* excutes, with output:
"34"
===== ✎ =====
~~DISCUSSION~~