Cartesian product of two or more lists: Difference between revisions
Content added Content deleted
(→{{header|F Sharp|F#}}: Claim the extra credit) |
|||
Line 599: | Line 599: | ||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
===The Task=== |
|||
<lang fsharp> |
<lang fsharp> |
||
//Nigel Galloway February 12th., 2018 |
//Nigel Galloway February 12th., 2018 |
||
Line 610: | Line 611: | ||
cP [] [1;2] -> [] |
cP [] [1;2] -> [] |
||
</pre> |
</pre> |
||
===Extra Credit=== |
|||
<lang fsharp> |
|||
//Nigel Galloway August 14th., 2018 |
|||
let cP ng=List.foldBack(fun n g->[for n' in n do for g' in g do yield n'::g']) ng [[]] |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
cP [[1;2];[3;4]] -> [[1; 3]; [1; 4]; [2; 3]; [2; 4]] |
|||
cP [[3;4];[1;2]] -> [[3; 1]; [3; 2]; [4; 1]; [4; 2]] |
|||
cP [[3;4];[]] ->[] |
|||
cP [[];[1;2]] ->[] |
|||
cP [[1776;1789];[7;12];[4;14;23];[0;1]] -> [[1776; 7; 4; 0]; [1776; 7; 4; 1]; [1776; 7; 14; 0]; [1776; 7; 14; 1]; |
|||
[1776; 7; 23; 0]; [1776; 7; 23; 1]; [1776; 12; 4; 0]; [1776; 12; 4; 1]; |
|||
[1776; 12; 14; 0]; [1776; 12; 14; 1]; [1776; 12; 23; 0]; [1776; 12; 23; 1]; |
|||
[1789; 7; 4; 0]; [1789; 7; 4; 1]; [1789; 7; 14; 0]; [1789; 7; 14; 1]; |
|||
[1789; 7; 23; 0]; [1789; 7; 23; 1]; [1789; 12; 4; 0]; [1789; 12; 4; 1]; |
|||
[1789; 12; 14; 0]; [1789; 12; 14; 1]; [1789; 12; 23; 0]; [1789; 12; 23; 1]] |
|||
cP [[1;2;3];[30];[500;100]] -> [[1; 30; 500]; [1; 30; 100]; [2; 30; 500]; [2; 30; 100]; [3; 30; 500]; [3; 30; 100]] |
|||
cP [[1;2;3];[];[500;100]] -> [] |
|||
</pre> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<lang Factor>IN: scratchpad { 1 2 } { 3 4 } cartesian-product . |
<lang Factor>IN: scratchpad { 1 2 } { 3 4 } cartesian-product . |