View Problem

Produce the combinations from two lists

Given two lists, produce the list of tuples formed by taking the combinations from the individual lists. E.g. given the letters ["a", "b", "c"] and the numbers [4, 5], produce the list: [["a", 4], ["b", 4], ["c", 4], ["a", 5], ["b", 5], ["c", 5]]
DiskEdit
python 2.5.2
[(x, y) for y in [1,2] for x in ['a','b','c']]
DiskEdit
python >= 2.6
import itertools
[x for x in itertools.product(["a", "b", "c"], [4, 5])]
DiskEdit
csharp .NET 3.5
using System.Collections.Generic;
public class ListCombiner {
public static void Main() {
var letters = new List<char>() { 'a', 'b', 'c' };
var numbers = new List<int>() { 1, 2, 3 };

// result is a list that contaings lists of objects
var result = new List<List<object>>();
foreach (var l in letters) {
foreach (var n in numbers) {
result.Add(new List<object>() { l, n });
}
}
}
}
ExpandDiskEdit
erlang
Combinations =
lists:foldl(fun (Number, Acc) -> Acc ++ lists:map(fun (Letter) -> {Letter, Number} end, Letters) end, [], Numbers),
ExpandDiskEdit
erlang
Combinations = lists:keysort(2, sofs:to_external(sofs:product(sofs:set(Letters), sofs:set(Numbers))))
DiskEdit
erlang
[[A, B] || A <- ["a", "b", "c"], B <- [4, 5]].

ExpandDiskEdit
fantom
[4,5].each |Int i| { ["a","b","c"].each |Str s| { r.add([i,s]) } }
ExpandDiskEdit
groovy
letters = ['a', 'b', 'c']
numbers = [4, 5]
combos = [letters, numbers].combinations()

Submit a new solution for python, csharp, erlang, fantom ...
There are 18 other solutions in additional languages (clojure, cpp, fsharp, haskell ...)