List comprehensions: Difference between revisions
Content added Content deleted
Line 852: | Line 852: | ||
{{Out}} |
{{Out}} |
||
<pre>[(3,4,5),(5,12,13),(6,8,10),(7,24,25),(8,15,17),(9,12,15),(12,16,20),(15,20,25)]</pre> |
<pre>[(3,4,5),(5,12,13),(6,8,10),(7,24,25),(8,15,17),(9,12,15),(12,16,20),(15,20,25)]</pre> |
||
Finally an alternative to the list comprehension from the beginning. First introduce all triplets: |
|||
<lang haskell>triplets n = [(x, y, z) | x <- [1 .. n], y <- [x .. n], z <- [y .. n]]</lang> |
|||
If we apply this to our list comprehension we get this tidy line of code: |
|||
<lang haskell>[(x, y, z) | (x, y, z) <- triplets n, x^2 + y^2 == z^2]</lang> |
|||
=={{header|Hy}}== |
=={{header|Hy}}== |