[Haskell-cafe] Building all possible element combinations from N lists.

Alex Stangl alex at stangl.us
Thu Oct 25 23:41:14 CEST 2012


On Fri, Oct 26, 2012 at 12:44:31AM +0400, dokondr wrote:
> I am looking for the algorithm and code better then the one I wrote (please
> Build all possible element combinations from N lists.
> Valid combination consists of k <= N elements.
> Where each element of a single combination is taken from one of the N
> lists.

combos [] = [[]]
combos ([]:ls) = combos ls
combos ((h:t):ls) = map (h:) (combos ls) ++ combos (t:ls)

Drop last element if you don't want to include the empty set. It
wouldn't be as elegant, but you can translate this to Java.

Alex



More information about the Haskell-Cafe mailing list