[Haskell-cafe] How to do the "permutation and combination" thing?

Magicloud Magiclouds magicloud.magiclouds at gmail.com
Mon Mar 15 03:37:20 EDT 2010


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) ]
  ... ]

On Mon, Mar 15, 2010 at 4:38 AM, Richard O'Keefe <ok at cs.otago.ac.nz> wrote:
> The first question is "what does 'all the combinations' actually MEAN?"
>
> We are told that
>
>        f [a,b,c] [d,e,f] [g,h,i] =
>          [[(a,d,g),(b,e,h),(c,f,i)], ...]
>
> in which the first element of the result is just
> zip3 [a,b,c] [d,e,f], [g,h,i].  But what are the
> other elements?  Why is "all the combinations" a list
> of lists of tuples rather than a list of tuples?
>
> At first I thought you were after
>        f xs ys zs = [(x,y,z) | x <- xs, y <- ys, z <- zs]
> because that's what I would mean by "all the combinations",
> but the example shows that's not so.
>
> When you can explain clearly what you mean by "all the
> combinations", the code won't be far away.
>
>
>



-- 
竹密岂妨流水过
山高哪阻野云飞


More information about the Haskell-Cafe mailing list