[Haskell-beginners] Fundamentals of List Comprehension and Recursion

Cody Haberkorn cody.haberkorn at gmail.com
Tue Jan 3 04:07:35 CET 2012

Hi all,

I high school programmer and just started learning Haskell. Haskell so far
seems to be an awesome language. I began reading "Learn you Haskell for a
Great Good!" I am confused with list comprehension, as I am coming from an
imperative programming language (Java). From my understanding,
list comprehension is synonymous to Java's for-loop and what not.

For example:

let xs = [1,2,3,4,5]
[x | x <- xs]

I believe it is saying that "bound x to xs, and do (print) x."

Another example:

let rightTriangles' = [ (a,b,c) | c <- [1..10] , b <- [1.. c], a <- [1..
b], a^2 + b^2 == c^2, a+b+c == 24]
ghci > rightTriangles'
[(6 ,8 ,10)]

I believe it is saying "store c with the numbers 1-10, b is length of c,
and a is the length of b. Print the answer (a,b,c) which satisfies a^2 +
b^2 == c^2, a+b+c == 24.
It may seem that I am answering my own question, but when it becomes more
complicated or I am ask to do specific tasks that involves list
comprehensions, I do not know how to read or compose one.

Second, can someone do a line by line breakdown of this code? The book does
a sub-par job explaining it.
-- Creating our own sum function
sum ' :: (Num a) => [a] -> a
sum ' [] = 0
sum ' (x:xs) = x + sum ' xs

Summed up: How did you learn Haskell and how long did it take before you
became competent? This is my second day and I am already running into
problems. I have to re-read things where as Java it came easy to me. Any
book or tutorials? Can someone explain list comprehension and my recursion

- Cody

p.s The questions so far have been over my head (is this a " true
beginner's email list"?), I'll try to answer what I can.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120102/5dba6db5/attachment.htm>

More information about the Beginners mailing list