[Haskell-cafe] Re: zip3, zip4 ... -> zipn?

apfelmus apfelmus at quantentunnel.de
Sat Aug 11 10:54:10 EDT 2007


Frank Buss schrieb:
> Is it possible to write a function like this:
> 
> zipn n list_1 list_2 list_3 ... list_n
> 
> which implements zip3 for n=3, zip4 for n=4 etc.? Looks like variable number
> of arguments are possible, like printf shows, so a general zipn should be
> possible, too. If it is possible, why there are functions like zip5 and not
> just zipn?

What type would this function have? It's not possible to formulate this 
type in Haskell98. The problem is that the number of arguments cannot be 
determined statically, i.e. it depends on the value of  n  at run-time. 
There are languages more freaky than Haskell (like Agda or Epigram ) 
that can do that (without dynamic typing, that is!), they are called 
"dependently typed".

However, type-class hackery (or type synonym families once they're 
available in GHC) can be used to do something like that if you give the 
value of n at compile-time. I won't dwell into that, though.

Also, applicative functors can help

   GHCi> :m +Control.Applicative
   GHCi> (\x y z -> x*(y+z)) <$> ZipList [1,2,3]
         <*> ZipList [-1,0,1] <*> ZipList [1,1,1]
   ZipList [0,2,6]
   GHCi>

(the second command is a single line.)

Regards,
apfelmus



More information about the Haskell-Cafe mailing list