List comprehensions: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Explicitly import guard)
(→‎{{header|Haskell}}: Add type signatures)
Line 808: Line 808:


=={{header|Haskell}}==
=={{header|Haskell}}==
<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 :: (Enum t, Eq t, Num t) => t -> [(t, t, t)]
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:
List-comprehensions in Haskell are in fact syntactic sugar for do-notation, so the above is equivalent to the following:
Line 814: Line 816:
<lang haskell>import Control.Monad (guard)
<lang haskell>import Control.Monad (guard)


pyth :: (Enum t, Eq t, Num t) => t -> [(t, t, t)]
pyth n = do
pyth n = do
x <- [1..n]
x <- [1..n]