Cartesian product
The Cartesian product operator, written >< (or, using Unicode,
as ×), operates on two collections of the same type (either
sets, bags, or lists), and
forms the collection of all possible pairs with one element taken from
the first collection and the other from the second.
On lists, the order matters: the resulting list has the first element of the first list matched with all elements of the second list, then the second element of the first list matched with all elements of the second list, and so on.
Disco> [2,1,1] >< [6,7] [(2, 6), (2, 7), (1, 6), (1, 7), (1, 6), (1, 7)]
On sets, we simply get the set of all unique pairs.
Disco> {2,1,1} >< {6,7} {(1, 6), (1, 7), (2, 6), (2, 7)}The behavior of Cartesian product on bags is slightly less intuitive, but follows directly from the fact that taking the Cartesian product of two lists and then converting the result to a bag always yields the same result as first converting the two lists to bags and then taking the Cartesian product (although the latter can be more efficient). That is, for all lists
l1andl2,bag(l1 >< l2) == bag(l1) >< bag(l2).In particular, if
ais an element of bagAwith a multiplicity ofm, andbis an element of bagBwith a multiplicity ofn, then(a,b)is an element ofA >< Bwith a multiplicity ofm * n. In other words, we havem * nways to form the pair(a,b)if we havemcopies ofato choose from andncopies ofbto choose from.Disco> bag([1,1,2,3]) >< bag([8,7,7,7]) ⟅(1, 7) # 6, (1, 8) # 2, (2, 7) # 3, (2, 8), (3, 7) # 3, (3, 8)⟆