List comprehensions: Difference between revisions

Content added Content deleted
No edit summary
m (alphabetize)
Line 4: Line 4:


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|Erlang}}==

pythag(N) ->
[ {A,B,C} ||
A <- lists:seq(1,N),
B <- lists:seq(1,N),
C <- lists:seq(1,N),
A+B+C =< N,
A*A+B*B == C*C
].


=={{header|Haskell}}==
=={{header|Haskell}}==
Line 25: Line 36:


TODO: Alternative with generators
TODO: Alternative with generators

=={{header|Erlang}}==

pythag(N) ->
[ {A,B,C} ||
A <- lists:seq(1,N),
B <- lists:seq(1,N),
C <- lists:seq(1,N),
A+B+C =< N,
A*A+B*B == C*C
].