Cartesian product of two or more lists: Difference between revisions
Cartesian product of two or more lists (view source)
Revision as of 20:15, 10 January 2023
, 1 year ago→{{header|ALGOL 68}}: Added a unary X operator to calculate the product of a list of lists
(Added Algol 68) |
(→{{header|ALGOL 68}}: Added a unary X operator to calculate the product of a list of lists) |
||
Line 282:
{}</pre>
=={{header|ALGOL 68}}==
Using a 1-dimensional array of INT to represent a list and a 2-dimensional array ( [,]INT ) to represent a product of two (or more) lists.
<br>
A list of lists is represented by a 1-dimensional array of 1-dimensional arrays of INT ([][]INT).
<syntaxhighlight lang="algol68">
BEGIN # Cartesian Product #
#
PRIO X = 7; #
# returns the Cartesian product of the lists a and b #
OP X = ( []INT a, b )[,]INT:
BEGIN
Line 303 ⟶ 306:
result
END # X # ;
# returns the Cartesian product of the Cartesian product a and list b #
OP X = ( [,]INT a, []INT b )[,]INT:
BEGIN
Line 319 ⟶ 323:
result
END # X # ;
# returns the Cartesian product of the lists in a #
IF UPB a <= LWB a
THEN # zero or 1 list #
[,]INT()
ELSE # 2 or more lists #
FLEX[ 1 : 0, 1 : 0 ]INT result := a[ LWB a ] X a[ LWB a + 1 ];
FOR i FROM LWB a + 2 TO UPB a DO
result := result X a[ i ]
OD;
result
FI # X # ;
# print a Cartesian product #
PROC print product = ( [,]INT p )VOID:
Line 347 ⟶ 363:
print( ( "]" ) )
END # print list # ;
BEGIN # test the X operators
# prints the product of two lists #
PROC print lxl = ( []INT a, b )VOID:
BEGIN
Line 355 ⟶ 371:
print( ( newline ) )
END # print lxl # ;
# prints the product of a list of lists #
▲ PROC print lxlxl = ( []INT a, b, c )VOID:
PROC print xll = (
IF LWB a
print
print( ( newline ) )
print lxl( ( 1, 2 ), ( 3, 4 ) );
print lxl( ( 3, 4 ), ( 1, 2 ) );
print lxl( ( 1, 2 ), () );
print lxl( (), ( 1, 2 ) );
print
print
print
END
END
|