Cartesian product of two or more lists: Difference between revisions
Content added Content deleted
m (Commenting Fōrmulæ solution, broken link) |
(→{{header|UNIX Shell}}: Add implementation.) |
||
Line 4,327: | Line 4,327: | ||
</pre> |
</pre> |
||
=={{header|UNIX Shell}}== |
|||
The UNIX shells don't allow passing or returning arrays from functions (other than pass-by-name shenanigans), but as pointed out in the Perl entry, wildcard brace expansion (in bash, ksh, zsh) does a Cartesian product if there's more than one set of alternatives. It doesn't handle the empty-list case (an empty brace expansion item is treated as a single item that is equal to the empty string), but otherwise it works: |
|||
$ printf '%s' "("{1,2},{3,4}")"; printf '\n' |
|||
(1,3)(1,4)(2,3)(2,4) |
|||
$ printf '%s' "("{3,4},{1,2}")"; printf '\n' |
|||
(3,1)(3,2)(4,1)(4,2) |
|||
More than two lists is not a problem: |
|||
$ printf '%s\n' "("{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) |
|||
$ printf '%s\n' "("{1,2,3},30,{500,100}")" |
|||
(1,30,500) |
|||
(1,30,100) |
|||
(2,30,500) |
|||
(2,30,100) |
|||
(3,30,500) |
|||
(3,30,100) |
|||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C#}} |
{{trans|C#}} |