Cartesian product of two or more lists: Difference between revisions

Content added Content deleted
(Erlang version)
Line 1,471: Line 1,471:


[]</pre>
[]</pre>
=={{header|Erlang}}==
Can do this with list comprehensions.
<syntaxhighlight lang="erlang">
-module(cartesian).
-export([product/2]).

product(S1, S2) -> [{A,B} || A <- S1, B <- S2].
</syntaxhighlight>
{{Out}}
<pre>
2> cartesian:product([],[1,2,3]).
[]
3> cartesian:product([1,2,3],[]).
[]
4> cartesian:product([1,2],[3,4]).
[{1,3},{1,4},{2,3},{2,4}]
5> cartesian:product([3,4],[1,2]).
[{3,1},{3,2},{4,1},{4,2}]
</pre>
=={{header|F Sharp|F#}}==
=={{header|F Sharp|F#}}==
===The Task===
===The Task===
Line 1,505: Line 1,524:
cP [[1;2;3];[];[500;100]] -> []
cP [[1;2;3];[];[500;100]] -> []
</pre>
</pre>

=={{header|Factor}}==
=={{header|Factor}}==
<syntaxhighlight lang="factor">IN: scratchpad { 1 2 } { 3 4 } cartesian-product .
<syntaxhighlight lang="factor">IN: scratchpad { 1 2 } { 3 4 } cartesian-product .