List comprehensions: Difference between revisions

No edit summary
Line 5:
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|ALGOL 68}}==
ALGOL 68 does not have list comprehension, however it is ''sometimes'' reasonably
generous about where a '''flex''' array is declared. And with the addition
of an append '''operator''' "+:=" for lists they can be similarly manipulated.
 
{{trans|Python}}
 
{{works with|ALGOL 68|Standard - no extensions to language used}}
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<lang algol>MODE XYZ = STRUCT(INT x,y,z);
 
OP +:= = (REF FLEX[]XYZ lhs, XYZ rhs)FLEX[]XYZ: (
[UPB lhs+1]XYZ out;
out[:UPB lhs] := lhs;
out[UPB out] := rhs;
lhs := out
);
 
INT n = 20;
print (([]XYZ(
FLEX[0]XYZ xyz;
FOR x TO n DO FOR y FROM x+1 TO n DO FOR z FROM y+1 TO n DO IF x*x + y*y = z*z THEN xyz +:= XYZ(x,y,z) FI OD OD OD;
xyz), new line
))</lang>
Output:
<pre>
+3 +4 +5 +5 +12 +13 +6 +8 +10 +8 +15 +17 +9 +12 +15 +12 +16 +20
</pre>
=={{header|Clojure}}==
(for [x (range 1 21) y (range x 21) z (range y 21) :when (= (+ (* x x) (* y y)) (* z z))] [x y z])