List comprehensions: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: List-comprehension is just sugar for do-notation.)
Line 810: Line 810:
<lang haskell>pyth n = [(x,y,z) | x <- [1..n], y <- [x..n], z <- [y..n], x^2 + y^2 == z^2]</lang>
<lang haskell>pyth n = [(x,y,z) | x <- [1..n], y <- [x..n], z <- [y..n], x^2 + y^2 == z^2]</lang>


List-comprehensions in Haskell are in fact syntactic sugar for do-notation, so the above is equivalent to the following:
Since lists are [http://haskell.org/haskellwiki/Monad monads], one can alternatively also use the do-notation (which is practical if the comprehension is large):


<lang haskell>import Control.Monad
<lang haskell>import Control.Monad