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

Larry Evans cppljevans at suddenlink.net
Sun Nov 30 12:25:30 EST 2008


On 11/23/08 13:52, Luke Palmer wrote:
> 2008/11/23 Larry Evans <cppljevans at suddenlink.net>:
>> http://www.muitovar.com/monad/moncow.xhtml#list
>>
>> contains a cross function which calculates the cross product
>> of two lists.  That attached does the same but then
>> used cross on 3 lists.  Naturally, I thought use of
>> fold could generalize that to n lists; however,
>> I'm getting error:
> 
> You should try writing this yourself, it would be a good exercise.  To
> begin with, you can mimic the structure of cross in that tutorial, but
> make it recursive.  After you have a recursive version, you might try
> switching to fold or foldM.
> 
> The type of the function will not involve tuples, since they can be
> arbitrary length (dynamic-length tuples are not supported in Haskell;
> we use lists for that).
> 
> cross :: [[a]] -> [[a]]
> 

However, list's contain elements all of the same type.  What the
following boost post:

   http://thread.gmane.org/gmane.comp.lib.boost.devel/182797/focus=182915

demonstrated was, AFAICT, the c++ template metaprogramming counterpart
to the moncow haskell cross.  Now, AFAICT, the boost vault directory:

 
http://www.boostpro.com/vault/index.php?PHPSESSID=ab51206c9d980155d142f5bcef8e00ee&direction=0&order=&directory=Template%20Metaprogramming

in the cross_nproduct_view_test.zip, contains what I'm looking for
in haskell. I'm guessing that:

   template<class Row, class Column>struct row_view;

corresponds to the haskell tuple type

   (row,column)

I'm trying to confirm that by printing out the typename in
a formated form, but I'm having trouble doing that at the
moment:

   http://preview.tinyurl.com/66x4nx

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

?



More information about the Haskell-Cafe mailing list