
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