
Matt Harden
zip :: ZipFunctor f => f a -> f b -> f (a,b) zip = zipWith (,) zip3 :: ZipFunctor f => f a -> f b -> f c -> f (a,b,c) zip3 = zipWith3 (,,)
One can easily create ZipFunctor instances for trees and other data structures. I can provide examples if you like. With multiple parameter type classes (MPTCs, they are not in Haskell 98) as well as functional dependencies (also not in h98), one can also create a "Zippable" class to generalize the zip function over multiple tuple types and eliminate zip3, zip4, etc.
can you explain how you do that ?? (i dont know what is functional dependencies, but i think i understand multiple parameter type classes and i dont see how do make something that avoid to define a zip3 zip4 ....

I won't try to explain functional dependencies, because I don't understand them all that well, there is documentation, and others on this list could explain them much better than I can. Here is an example of how to implement a polymorphic zip (used together with the ZipFunctor I defined earlier):
class Zippable a f b | a -> f b, f b -> a where zip :: a -> f b
instance (ZipFunctor f) => Zippable (f a,f b) f (a,b) where zip (xs,ys) = fmap (,) xs `zap` ys
instance (ZipFunctor f) => Zippable (f a,f b,f c) f (a,b,c) where zip (xs,ys,zs) = fmap (,,) xs `zap` ys `zap` zs
instance (ZipFunctor f) => Zippable (f a,f b,f c,f d) f (a,b,c,d) where zip (xs1,xs2,xs3,xs4) = fmap (,,,) xs1 `zap` xs2 `zap` xs3 `zap` xs4
-- Hopefully you can see how to define instances for more tuples
Nothing stops us from adding `unzip` to the class as well, but I left that out to keep it short. Without functional dependencies, the type system would have difficulty with type inference, and we would have to put type declarations all over the place when using these classes. By the way, I should point out that these zip functions aren't used exactly the same way as the current zip functions. They convert a tuple of lists into a list of tuples. So, here's a demonstration of the difference:
-- here is the old zip3 function zip3 [1..5] "abcd" [0.0,3.14] == [(1,'a',0.0),(2,'b',3.14)]
-- and here is the new zip zip ([1..5],"abcd",[0.0,3,14]) == [(1,'a',0.0),(2,'b',3.14)]
Yoann Padioleau wrote:
Matt Harden
writes: zip :: ZipFunctor f => f a -> f b -> f (a,b) zip = zipWith (,) zip3 :: ZipFunctor f => f a -> f b -> f c -> f (a,b,c) zip3 = zipWith3 (,,)
One can easily create ZipFunctor instances for trees and other data structures. I can provide examples if you like. With multiple parameter type classes (MPTCs, they are not in Haskell 98) as well as functional dependencies (also not in h98), one can also create a "Zippable" class to generalize the zip function over multiple tuple types and eliminate zip3, zip4, etc.
can you explain how you do that ?? (i dont know what is functional dependencies, but i think i understand multiple parameter type classes and i dont see how do make something that avoid to define a zip3 zip4 ....
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (2)
-
Matt Harden
-
Yoann Padioleau