Jump to content

List comprehensions: Difference between revisions

Haskell: Fixed missing n; added do-notation variant
m (Added missing parenthesis)
(Haskell: Fixed missing n; added do-notation variant)
Line 7:
=={{header|Haskell}}==
 
pyth n = [(x,y,z) | x <- [1..20n], y <- [x..20n], z <- [y..20n], x^2 + y^2 == z^2]
 
Since lists are [[Monads]], one can alternatively also use the do-notation (which is practical if the comprehension is large):
 
import Control.Monad
pyth n = do
x <- [1..n]
y <- [x..n]
z <- [y..n]
guard $ x^2 + y^2 == z^2
return (x,y,z)
 
=={{header|Python}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.