6 ms·
Negative, the Visitor pattern cannot be naturally replaced with map and fmap. Read up on double dispatch at e.g.: http://en.wikipedia.org/wiki/Double_dispatch h
by scscsc 14y ago
Negative, the Visitor pattern cannot be naturally replaced with map and fmap. Read up on double dispatch at e.g.: http://en.wikipedia.org/wiki/Double_dispatch http://en.wikipedia.org/wiki/Double_dispatch
- TeMPOraL 14y agoBut then neither of those problems exist in Common Lisp, as you have both first-order functions and multiple-dispatch in object model :).
- chongli 14y agoMaybe not in Haskell, but Clojure's multimethods handle this just fine.
- riffraff 14y agothat means that the visitor pattern can be replaced with multiple dispatch (true) but neither map nor fmap imply that (and in fact many languages have the latter but not the former)
- chongli 14y agoClojure has both map and multimethods. It's trivial to map over a collection and dispatch to a different implementation for every element in the collection. With multiple collections, this extends to multiple dispatch over the corresponding elements in each collection.
- riffraff 14y agoI will rephrase myself: the original statement is you can do V with F to which the reply was no, you can't do V with F and the subsequent reply was but you can do V with M and I pointed out that F does not imply V. Your current reply is Clojure has both F and M and can do V which is obvious, since M implies V already, but still unrelated to what F can do.
- Peaker 14y agoHaskell's type classes generalize further than multi methods do. You get dispatch based on any part of the type signature, not just the arguments.
- chongli 14y agoNot further than Clojure's multimethods. They dispatch on the value of an arbitrary function you write. This can be anything; it's not even restricted to types. Couple this with Clojure's ad hoc hierarchy system and even Haskell's types seem limited by comparison.
- Peaker 14y agoCan you implement the Read class in Clojure? class Read r where read :: String -> r (Let's ignore parse errors for our purposes here) Haskell supports return-type polymorphism. Clojure, being dynamically typed, cannot in general support this kind of overloading.
- chongli 14y agoGood point, though Clojure's extensible reader takes care of most of the use for this. I think it's a trade-off though. Can Haskell dispatch on a value rather than a type?
- Peaker 14y agoNote that this feature is far from being useful only for class Read. The Monad class actually depends on it too: class Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a The type of "return" can only be expressed with return-type polymorphism. This means you cannot really implement Haskell-like monads with dynamic typing. You have to implement them in a less general way. As for dispatching on runtime values, that is ordinary pattern matching: f (RuntimeValue1 x y) = ... case 1 ... f (RuntimeValue2 x y) = ... case 2 ...
- 14y ago
- tikhonj 14y agoDouble-dispatch seems to be equivalent to Haskell's pattern matching. Since Haskell does not have sub-typing, everything has the same runtime type. However, you can easily combine different types into a single algebraic data type and then dispatch by pattern matching against that type. This probably isn't exactly the same as double-dispatch in other languages, but it seems to be very similar and to serve the same purpose.
- lelf 14y agoMany kinds of visitor pattern are just fmap (better call it Functor). Here is the good explanation: http://www.haskell.org/pipermail/haskell-cafe/2009-June/062352.html http://www.haskell.org/pipermail/haskell-cafe/2009-June/0623... Haskell people are good at math