List comprehensions: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: (pure and empty in lieu of return and mzero)) |
|||
Line 835: | Line 835: | ||
and both of the above could be de-sugared to: |
and both of the above could be de-sugared to: |
||
<lang haskell>import Control. |
<lang haskell>import Control.Applicative (empty) |
||
pyth :: Int -> [(Int, Int, Int)] |
pyth :: Int -> [(Int, Int, Int)] |
||
Line 845: | Line 845: | ||
[y .. n] >>= |
[y .. n] >>= |
||
\z -> |
\z -> |
||
case x ^ 2 + y ^ 2 == z ^ 2 of |
case (((x ^ 2) + (y ^ 2)) == (z ^ 2)) of |
||
True -> |
True -> pure (x, y, z) |
||
False -> |
False -> empty</lang> |
||
which can be further specialised (given the particular context of the list monad, in which >>= is concatMap) to: |
which can be further specialised (given the particular context of the list monad, in which >>= is concatMap, pure is flip (:) [], and empty is []) to: |
||
<lang haskell>pyth :: Int -> [(Int, Int, Int)] |
<lang haskell>pyth :: Int -> [(Int, Int, Int)] |