[Haskell-cafe] How to do the "permutation and combination" thing?
Daniel Fischer
daniel.is.fischer at web.de
Mon Mar 15 04:39:44 EDT 2010
Am Montag 15 März 2010 08:37:20 schrieb Magicloud Magiclouds:
> Sorry, I did not make it clear, since I did not know how to say this
> in technical terms.
> With comprehension, I could get all the possibilities that "draw one
> elem from each list and put them together". But consider this: for
> example, there are two types of pet, dog and cat. And there are two
> persons, him and her. So "how many kinds of matches are there
> (orderless)?" The answer is two: "him with dog and her with cat, him
> with cat and her with dog". So
> f [a, b, c] [d, e, f] [g, h, i] =
> [ [ (a, d, g), (b, e, h), (c, f, i) ]
> , [ (a, d, g), (b, e, i), (c, f, h) ]
> , [ (a, d, h), (b, e, i), (c, f, g) ]
> , [ (a, d, h), (b, e, g), (c, f, i) ]
> , [ (a, d, i), (b, e, g), (c, f, h) ]
> , [ (a, d, i), (b, e, h), (c, f, g) ]
> ... ]
>
In both, your verbal example and the pseudo-code example, all the groups
have the same number of members (equal to the number of groups, which may
or may not be coincidental).
Is that a precondition, that all groups have the same number of members?
If so, would the desired result for three groups of two members each be
f3 [a,b] [c,d] [e,f] =
[ [ (a,c,e), (b,d,f) ]
, [ (a,c,f), (b,d,e) ]
, [ (a,d,e), (b,c,f) ]
, [ (a,d,f), (b,c,e) ]
]
and for two groups of three members each
f2 [a,b,c] [d,e,f] =
[ [ (a,d), (b,e), (c,f) ]
, [ (a,d), (b,f), (c,e) ]
, [ (a,e), (b,d), (c,f) ]
, [ (a,e), (b,f) , (c,d) ]
, [ (a,f), (b,d), (c,e) ]
, [ (a,f), (b,e), (c,d) ]
]
?
In that case, look at Data.List.permutations and the zipN functions.
More information about the Haskell-Cafe
mailing list