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

Larry Evans cppljevans at suddenlink.net
Sun Nov 30 13:04:18 EST 2008

On 11/30/08 11:30, Luke Palmer wrote:
> 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.

Thanks Luke.  I'll try that.

> 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 following post:


shows at least one person that would find it useful, at least in
c++.  Of course maybe it would be less useful in haskell.

More information about the Haskell-Cafe mailing list