List comprehensions: Difference between revisions

Content added Content deleted
No edit summary
m (Fixed lang tags (using MediaWiki::API).)
Line 22: Line 22:


{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.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);
<lang algol68>MODE XYZ = STRUCT(INT x,y,z);


OP +:= = (REF FLEX[]XYZ lhs, XYZ rhs)FLEX[]XYZ: (
OP +:= = (REF FLEX[]XYZ lhs, XYZ rhs)FLEX[]XYZ: (
Line 43: Line 43:


=={{header|C++}}==
=={{header|C++}}==
<lang C++>
<lang cpp>#include <vector>
#include <vector>
#include <cmath>
#include <cmath>
#include <iostream>
#include <iostream>
Line 76: Line 75:
}
}
This produces the following output:
This produces the following output:
3 4 5 5 12 13 6 8 10 8 15 17 9 12 15 12 16 20
3 4 5 5 12 13 6 8 10 8 15 17 9 12 15 12 16 20</lang>
</lang>


=={{header|Clojure}}==
=={{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])
<lang lisp>(for [x (range 1 21) y (range x 21) z (range y 21) :when (= (+ (* x x) (* y y)) (* z z))] [x y z])</lang>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 112: Line 110:
=={{header|E}}==
=={{header|E}}==


pragma.enable("accumulator") # considered experimental
<lang e>pragma.enable("accumulator") # considered experimental

accum [] for x in 1..n { for y in x..n { for z in y..n { if (x**2 + y**2 <=> z**2) { _.with([x,y,z]) } } } }
accum [] for x in 1..n { for y in x..n { for z in y..n { if (x**2 + y**2 <=> z**2) { _.with([x,y,z]) } } } }</lang>


=={{header|Erlang}}==
=={{header|Erlang}}==


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


=={{header|Haskell}}==
=={{header|Haskell}}==
Line 140: Line 138:


=={{header|J}}==
=={{header|J}}==
<lang J>(#~ (2&{ = 1&{ +&.:*: 0&{)@|:)@(1+3&# #: i.@^&3)</lang>
<lang J>(#~ (2&{ = 1&{ +&.:*: 0&{)@|:)@(1+3&# #: i.@^&3)</lang>


The idiom here has two major elements:
The idiom here has two major elements:
Line 179: Line 177:


=={{header|Mathematica}}==
=={{header|Mathematica}}==
<nowiki>Select[Tuples[Range[n], 3], #1[[1]]^2 + #1[[2]]^2 == #1[[3]]^2 &]</nowiki>
<lang mathematica>Select[Tuples[Range[n], 3], #1[[1]]^2 + #1[[2]]^2 == #1[[3]]^2 &]</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==
Line 218: Line 216:
List comprehension:
List comprehension:


<lang python>[(x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2]</lang>
<lang python>[(x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2]</lang>


A Python generator comprehension (note the outer round brackets), returns an iterator over the same result rather than an explicit list:
A Python generator comprehension (note the outer round brackets), returns an iterator over the same result rather than an explicit list:


<lang python>((x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2)</lang>
<lang python>((x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2)</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 285: Line 283:
TI-89 BASIC does not have a true list comprehension, but it has the seq() operator which can be used for some similar purposes.
TI-89 BASIC does not have a true list comprehension, but it has the seq() operator which can be used for some similar purposes.


{1, 2, 3, 4} → a
<lang ti89b>{1, 2, 3, 4} → a
seq(a[i]^2, i, 1, dim(a))
seq(a[i]^2, i, 1, dim(a))</lang>


produces {1, 4, 9, 16}. When the input is simply a numeric range, an input list is not needed; this produces the same result:
produces {1, 4, 9, 16}. When the input is simply a numeric range, an input list is not needed; this produces the same result:


seq(x^2, x, 1, 4)
<lang ti89b>seq(x^2, x, 1, 4)</lang>