List comprehensions: Difference between revisions
Content added Content deleted
No edit summary |
m (Added missing parenthesis) |
||
Line 3: | Line 3: | ||
A [http://http://en.wikipedia.org/wiki/List_comprehension list comprehension] is a special syntax in some programming languages to describe lists. It is similar to the way mathematicians describe sets, with a ''set comprehension'', hence the name. |
A [http://http://en.wikipedia.org/wiki/List_comprehension list comprehension] is a special syntax in some programming languages to describe lists. It is similar to the way mathematicians describe sets, with a ''set comprehension'', hence the name. |
||
Write a list comprehension that builds the list of all pythagorean triples with elements between 1 and n. If the language has multiple ways for expressing such a construct for example, direct list comprehensions and generators), write one example for each. |
Write a list comprehension that builds the list of all pythagorean triples with elements between 1 and n. If the language has multiple ways for expressing such a construct (for example, direct list comprehensions and generators), write one example for each. |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
Revision as of 09:47, 16 November 2007
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
A list comprehension is a special syntax in some programming languages to describe lists. It is similar to the way mathematicians describe sets, with a set comprehension, hence the name.
Write a list comprehension that builds the list of all pythagorean triples with elements between 1 and n. If the language has multiple ways for expressing such a construct (for example, direct list comprehensions and generators), write one example for each.
Haskell
pyth n = [(x,y,z) | x <- [1..20], y <- [x..20], z <- [y..20], x^2 + y^2 == z^2]
Python
[(x,y,z) for x in xrange(1,21) for y in xrange(x,21) for z in xrange(y,21) if x**2 + y**2 == z**2]