another list comprehesion error

hello, I know try to make this work. The user enters a number and the programm calculates all the numbers which are smaller then the number and where x^2+Y^2=Z^2. So i Thought this would work. roelof :: a -> b -> c -> (a,b,c) roelof n = [(x y z) | x^2+Y^2=Z^2 <- x<-[1..n], y<- [1..n], z<-[1..n]] But I get this error : oefening.hs:2:30: parse error on input `=' Roelof

On Jul 21, 2011, at 1:27 PM, Roelof Wobben wrote:
roelof :: a -> b -> c -> (a,b,c) roelof n = [(x y z) | x^2+Y^2=Z^2 <- x<-[1..n], y<- [1..n], z<-[1..n]]
But I get this error : oefening.hs:2:30: parse error on input `='
Hi, Roelof. This short amount of code has many syntax and semantic errors. May I suggest that you might benefit from trying to make this function using only normal function application and the basic list functions. I think you will learn more about Haskell this way. Personally, i rarely use list comprehensions. I think they are not very modular. Also, it might help people to understand your background a little. This way comments could be better focused. For instance, which other programming languages do you know? ____________________ David Place Owner, Panpipes Ho! LLC http://panpipesho.com d@vidplace.com

Oke, So I can better goto the next chapter or look for a better book. Im using now Programming in Haskell. My background is that many years I have tried to programm in Delphi. The last years I have tried C and C++ but these were to difficult for me with header files. I could make this with a for next loop and a if then if you like it that way ? Roelof ________________________________
Subject: Re: [Haskell-beginners] another list comprehesion error From: d@vidplace.com Date: Thu, 21 Jul 2011 14:05:14 -0400 CC: beginners@haskell.org To: rwobben@hotmail.com
On Jul 21, 2011, at 1:27 PM, Roelof Wobben wrote:
roelof :: a -> b -> c -> (a,b,c) roelof n = [(x y z) | x^2+Y^2=Z^2 <- x<-[1..n], y<- [1..n], z<-[1..n]]
But I get this error : oefening.hs:2:30: parse error on input `='
Hi, Roelof.
This short amount of code has many syntax and semantic errors. May I suggest that you might benefit from trying to make this function using only normal function application and the basic list functions. I think you will learn more about Haskell this way. Personally, i rarely use list comprehensions. I think they are not very modular.
Also, it might help people to understand your background a little. This way comments could be better focused. For instance, which other programming languages do you know?
____________________ David Place Owner, Panpipes Ho! LLC http://panpipesho.com d@vidplace.commailto:d@vidplace.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 ]
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.

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

Hi! On 21.07.2011 21:29, Roelof Wobben wrote:
roelof n x = [x | y<- [1..n]]
Have you tried this? What do you get for, say "roelof 4 5" ? Why? Now try this instead: roelof' n = [x | x <- [1..n]] What do you get for "roelof' 4" ?
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.
Hm, in the terminology of the book you're using the list comprehension [x | x <- [1..n]] reads "all x such that x drawn from [1..n]" So, you have a result (x) and a generator (x <- [1..n]). What you call "filter" is called "guard" in the book. So actually you have: [ result | generator(s) ] or (with guards): [ result | generator(s), guards(s) ] So, note that the comma (,) is a mere separator and the left arrow (<-) forms part of the generators (everything else is a guard or an error). Especially the term "input" is rather misleading. Generators are explained in chapter 5.1 (page 38f), guards are explained in 5.2 (page 39f). I'm not sure I can explain better than the book. Maybe you should experiment with generators first and then advance to the guards. A few suggestions (without guards): The list of the first 5 natural numbers. The list of the first 5 odd numbers. The list of all pairs (a, b) where a,b > 0, a <= b and b <= 5 Now you can try the last two with guards. HTH, Thomas
participants (3)
-
David Place
-
Roelof Wobben
-
Thomas