[Haskell-cafe] Re: howto tuple fold to do n-ary cross product?

Luke Palmer lrpalmer at gmail.com
Sun Nov 30 12:30:26 EST 2008

On Sun, Nov 30, 2008 at 10:25 AM, Larry Evans <cppljevans at suddenlink.net> wrote:
> Is there some version of haskell, maybe template haskell,
> that can do that, i.e. instead of:
>  cross::[[a]] -> [[a]]
> have:
>  crossn::[a0]->[a1]->...->[an] -> [(a0,a1,...,an)]

Ah yes!  This is straightforward usage of the list monad.  I suggest
applicative notation:

  import Control.Applicative
  (,,,) <$> xs0 <*> xs1 <*> xs2 <*> xs3

Or alternatively:

  import Control.Monad
  liftM4 (,,,) xs0 xs1 xs2 xs3

(I would have used liftA4, but it's not defined.  The definition looks
a lot like the first example :-)

This notation seems a bit magical, but you can build what you want
using a simple binary cross:

  cross :: [a] -> [b] -> [(a,b)]

It's just kind of a pain  (you build [(a,(b,(c,d)))] and then flatten
out the tuples).  The applicative notation is a neat little trick
which does this work for you.

If you're asking whether crossn, as a single function which handles
arbitrarily many arguments, can be defined, the short answer is "no".
I dare you to come up with a case in which such function adds more
than cursory convenience.

The long answer is "yes, but you don't want to".  It involves mad
typeclass hackery, and it doesn't buy you very much.


More information about the Haskell-Cafe mailing list