[Haskell-beginners] another list comprehesion error

Roelof Wobben rwobben at hotmail.com
Thu Jul 21 21:29:48 CEST 2011




----------------------------------------
> Subject: Re: [Haskell-beginners] another list comprehesion error
> From: d at vidplace.com
> Date: Thu, 21 Jul 2011 15:19:54 -0400
> CC: beginners at haskell.org
> To: rwobben at hotmail.com
>
> 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 ]
> >
>


 

Oke, I see that x<[1..n] part is now in the middle.

That I find quite confusing.

 

When I have made a list compresshion where I must print out a text I look like this ;

 

roelof n x = [x | y <- [1..n]]

 

So I thought the syntax of a list compression would be [output | filter <- input] 

But the answer is now  [ output | input <- filter] This is very confusing for me.

 

 

>
> 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.
>
>


I thought of doing something like this in pseudo code :

 

For teller = 1 to n 

    for teller2 = 1 to n 

         antwoord = (teller1 ^2 + teller^2)^ 0.5

            if antwoord == int(antwoord) then print x,y,z

 

 

 

 

Roelof

  

         

   		 	   		  


More information about the Beginners mailing list