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.Monad (mzero)
<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 -> return (x, y, z)
True -> pure (x, y, z)
False -> mzero</lang>
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)]