I'm currently learning Haskell from the Learn You a Haskell for Great Good! book. One of the examples involves creating 3-item tuples representing triangles:
let triangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10] ]
We then apply some conditions to the original list comprehension to only create right-angled triangles:
let rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2]
Producing result:
ghci> rightTriangles
[(3,4,5),(6,8,10)]
I would have produced this result via:
let rightTriangles = [(a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10], a^2 + b^2 == c^2, a < b && b < c]
Can anyone explain exactly how the tutorial's right angle code works? Both methods apply the filter to make sure that a < b && b < c but I just can't quite picture exactly what is happening with c <- [1..10], b <- [1..c], a <- [1..b].
I would be supremely grateful if someone could explain what's happening, perhaps with an some kind of illustration if possible. I'm quite new to functional programming and would really like to understand how this works.
Thanks!