Generates pairs from a list of lists based upon the first list:
fun generateTuples (lol) =
let
(* Treat the ListOfLists as a database
table, with the first
list being a key value column *)
val keys = hd(lol)
(* and the remaining columns being
additional fields in its tuple *)
val records = tl(lol)
(* Pairs the key with each column
in its record *)
fun formPairs (aKey,listOfRecords) =
if null listOfRecords
then []
else [aKey,hd(hd listOfRecords)]::
(formPairs(aKey,tl(listOfRecords)))
(* Pops a row's data fields from the record *)
fun chopLists (listOfRecords)=
if null listOfRecords
then []
else tl(hd(listOfRecords))::
(chopLists(tl(listOfRecords)))
in
(* Pass the first key value to formPairs
along with all the records. Then pop
the first row off the database and call
generateTuples on the remain *)
if null keys
then []
else generateTuples(tl(keys)::(chopLists(records)))
@ formPairs(hd(keys),records)
end
Example:
val list1 = [0,1,2,3,4]
val list2 = [55,66,77,88,99]
val list3 = [10,11,12,13,14]
val list4 = [555,666,777,888,999]
val lols = [list1,list2,list3,list4]
- generateTuples(lols);
val it =
[[4,99],[4,14],[4,999],[3,88],[3,13],[3,888],[2,77],[2,12],[2,777],[1,66],
[1,11],[1,666],...] : int list list