[Haskell-beginners] another list comprehesion error

David Place d at vidplace.com
Thu Jul 21 21:19:54 CEST 2011


On Jul 21, 2011, at 2:47 PM, Roelof Wobben wrote:

> I could make this with a for next loop and a if then  if you like it that way ?

I though it would be trivial, and perhaps it is.  This seems to be a case where the list comprehensions give quite a bit of help.

First of all, here is your function with all the syntax and semantic bugs fixed.  Please compare it closely to your version.

> roelof :: Int -> [(Int,Int,Int)]
> roelof n = [(x, y, z) | x<-[1..n], y<- [1..n], z<-[1..n],  x^2+y^2 == z^2 ]
> 


Here's a version using only functions without any of the syntactic sugar of list comprehensions.

> triples xs ys zs = concatMap (\x -> concatMap (\y -> (map (\z -> (x,y,z)) zs)) ys) xs
> roelof' n = filter (\(x,y,z) -> x^2+y^2 == z^2) $ triples [1..n] [1..n] [1..n]


It's quite unattractive.  Please someone tell me there is a better way.





More information about the Beginners mailing list