Friday, May 18, 2007

Combinations

It seems to be simple, but it took me two hours to write this function. And now I know the true nature of things!
combr :: [[a]] -> [[a]]
combr [l] = map (\x -> [x]) l
combr (l : tl) = concat $ map (\ll -> map (:ll) l) $ combr tl

print $ combr [[1,2], [3,4], [5,6,7]]

[[1,3,5],[2,3,5],[1,4,5],[2,4,5],[1,3,6],[2,3,6],[1,4,6],
[2,4,6],[1,3,7],[2,3,7],[1,4,7],[2,4,7]]

No comments:

Post a Comment