Hi all,<div><br></div><div>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. </div>
<div><br></div><div>For example: </div><div><br></div><div><font face="'courier new', monospace">let xs = [1,2,3,4,5]</font></div><div><font face="'courier new', monospace">[x | x <- xs]</font></div><div>
<br></div><div>I believe it is saying that "bound x to xs, and do (print) x." </div><div><br></div><div>Another example:</div><div><br></div><div><div><font face="'courier new', monospace">let rightTriangles' = [ (a,b,c) | c <- [1..10] , b <- [1.. c], a <- </font><span style="font-family:'courier new',monospace">[1.. b], a^2 + b^2 == c^2, a+b+c == 24]</span></div>
<div><font face="'courier new', monospace">ghci > rightTriangles'</font></div><div><font face="'courier new', monospace">[(6 ,8 ,10)]</font></div><div><br></div><div>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.</div>
<div>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. </div></div><div><br>
</div><div>Second, can someone do a line by line breakdown of this code? The book does a sub-par job explaining it.</div><div><font face="'courier new', monospace">-- Creating our own sum function</font></div><div>
<div><font face="'courier new', monospace">sum ' :: (Num a) => [a] -> a</font></div><div><font face="'courier new', monospace">sum ' [] = 0</font></div><div><font face="'courier new', monospace">sum ' (x:xs) = x + sum ' xs</font></div>
</div><div><br></div><div>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 code?</div>
<div><br></div><div>Thanks,</div><div>- Cody</div><div><br></div><div>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.</div><div>
<br></div>