       combination problem solved with Mathematica

• To: mathgroup at yoda.physics.unc.edu
• Subject: combination problem solved with Mathematica
• From: gaylord at ux1.cso.uiuc.edu
• Date: Mon, 30 Mar 1992 03:45:36 -0600

```=========================

the following query appeared in comp.lang.functional some time ago

"given a tuple of lists, generate a list of tuples with each component of
each
output tuple chosen from one of the components of the corresponding
tuple lists.  In other words, given

([1,2], [3,4,5])

as input, the program should produce

[(1,3), (1,4), (1,5), (2,3), (2,4), (2,5)]

The request was for a program written in some functional language
such as Standard ML.

======================================

Andrew Koenig's recent response in comp.lang.functional was limited to (ML)
lists because

" the original problem exactly because it can't be solved in that form
in ML: there is no way to write an ML program that decomposes arbitrary
tuples "

==================================

The functions defined by Andrew Koenig in his solution are easily
duplicated in Mathematica, which is a functional programming language (as
well as a rewrite system) and are not limited by datatype

it is even simpler to use one of Mathematica's built-in 'functions'

list1 = {a, 2};
list2 =  {cat, g, dog, 4};

Distribute[List[list1, list2], List]
{{a, cat}, {a, g}, {a, dog},  {a, 4}, {2, cat}, {2, g},  {2, dog}, {2, 4}}

Flatten[Outer[List, list1, list2], 1]
{{a, cat}, {a, g}, {a, dog},  {a, 4}, {2, cat}, {2, g},  {2, dog}, {2, 4}}

Other related built-in functions are:

Nest,  Fold , Map, Thread (a 'zip'operation), Inner.

Finally, one can play around (and avoid work) by doing all sorts of 'wierd'
things:
eg.,
(1) join list1 and list2 to create the list {a, 2, cat, g, dog, 4}

(2) create all of the combinations of this list taken two at a time [this
was recently done in three different ways in the uiuc.mathematica and
sci.math.symbolic groups and would be interesting to do in ML (for lists)
as well]

(3) select by pattern matching, those pairs whose first elements belong to
list1

```

• Prev by Date: Re: combination problem solved with Mathematica
• Next by Date: Re: combination problem solved with Mathematica
• Previous by thread: Re: combination problem solved with Mathematica
• Next by thread: Re: combination problem solved with Mathematica