In some applications you need to process all combinations of 2 lists.
The so-called cross product takes each element of the first list and combines it with each element of the second list.
Let’s say you want to create all the combinations of a computer with disk size and memory.
Hard disks = “512 MB¶1 TB¶2 TB”
Memory = “8 GB¶32 GB¶64 GB”
The result of a combination would be
512 MB|8 GB
512 MB|32 GB
512 MB|64 GB
1 TB|8 GB
1 TB|32 GB
1 TB|64 GB
2 TB|8 GB
2 TB|32 GB
2 TB|64 GB
As separator in this case have used a pipe “|”.
Very helpful are also a prefix and a postfix
*512 MB|8 GB#
*512 MB|32 GB#
*512 MB|64 GB#
*1 TB|8 GB#
*1 TB|32 GB#
*1 TB|64 GB#
*2 TB|8 GB#
*2 TB|32 GB#
*2 TB|64 GB#
For this task, once again a recusive function offers itself as a solution approach.
In this case, however, we must nest 2 recursions inside each other.
The first recursion takes the first list element of the first list and interacts through the second list by linking the element with prefix, separator and postfix with the respective element from the second list.
Then this linking is repeated with the other elements of the first list.
The custom function for this task looks like this:
ListCrossproduct
// ListCrossproduct ( List1; List2 ; Prefix ; Separator ; Postfix )
Let (
[
max = ValueCount ( List1 ) ;
maxj = ValueCount ( List2 )
] ;
SetRecursion (
While (
[ i = 1 ; out = "" ] ;
i ≤ max ;
[
out = List ( out ;
SetRecursion (
While (
[ j = 1 ; outj = "" ] ;
j ≤ maxj ;
[
outj = List ( outj ; Prefix & GetValue ( List1 ; i ) & Separator & GetValue ( List2 ; j ) & Postfix ) ;
j = j + 1
] ;
outj
) ;
maxj )
) ; // end inner recursion
i = i + 1
] ;
out
) ;
max )
)
You have any questions? Feel free to send me an email, I’ll try to answer as soon as possible.