set - Understanding A bit of Haskell -
i have quick question haskell. i've been following learn haskell, , bit confused execution order / logic of following snippet, used calculate side lengths of triangle, when sides equal or less 10 , total perimeter of triangle 24:
[(a,b,c) | c <- [1..10], b <- [1..c], <- [1..b], a^2 + b^2 == c^2, a+b+c==24]
the part confusing me upper expansion bound on b
, a
binding. gather, ..c
, ..b
used remove additional permutations (combinations?) of same set of triangle sides.
when run ..c/b
, answer:
[(6,8,10)]
when don't have ..c/b
:
[(a,b,c) | c <- [1..10], b <- [1..10], <- [1..10], a^2 + b^2 == c^2, a+b+c==24]
as didn't when typed in, got:
[(8,6,10),(6,8,10)]
which representative of same triangle, save a
, b
values have been swapped.
so, can walk me through logic / execution / evaluation of what's going on here?
the original version considers triplets (a,b,c) c number between 1 , 10, b number between 1 , c , number between 1 , b. (6,8,10) fits criteria, (8,6,10) doesn't (because here 8 , b 6, isn't between 0 , 6).
in version consider triplets (a,b,c) a, b , c between 1 , 10. make no restrictions on how a, b , c relate each other, (8, 6, 10) not excluded since numbers in indeed between 1 , 10.
if think of in terms of imperative for-loops, version this:
for c 1 10: b 1 10: 1 10: if a^2 + b^2 == c^2 , a+b+c==24: add (a,b,c) result
while original version this:
for c 1 10: b 1 c: c 1 b: if a^2 + b^2 == c^2 , a+b+c==24: add (a,b,c) result
Comments
Post a Comment