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

2007-08-13 Thread Brent Yorgey
On 8/11/07, Per Vognsen <[EMAIL PROTECTED]> wrote:
>
> Applicative functors can indeed help:
>
> (,,,) <$> [1,2,3] <*> [-1,0,1] <*> [1,1,1] <*> [0,2,6]
>
> You just use n-1 commas when you want the effect of zipn.


Actually, that's not quite right, since that uses the applicative functor
related to the list monad (so you get a list of 4-tuples of all possible
combinations, rather than a 4-way zip).  To get the zip behavior, you need
to add a ZipList constructor in front of all the lists, and then apply
getZipList at the end to extract.

-Brent
___
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2007-08-11 Thread Marc Weber
> 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>
http://www.soi.city.ac.uk/~ross/papers/Applicative.pdf
quote "The general scheme is as follows:"
(page 2)

Marc
___
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2007-08-11 Thread Per Vognsen
On 8/11/07, apfelmus <[EMAIL PROTECTED]> wrote:
> 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.)

Applicative functors can indeed help:

(,,,) <$> [1,2,3] <*> [-1,0,1] <*> [1,1,1] <*> [0,2,6]

You just use n-1 commas when you want the effect of zipn.

-Per
___
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe