|
|
|
|
|
by jrockway
5971 days ago
|
|
But that's not what monads are, what you have without a type system is just "calling a function with the result of another function". You could use the function composition operator to the same effect. Having also implemented a monad library in a dynamically-typed language, I am well aware of what happens when one operand of bind returns the wrong type. The rest of the computation silently proceeds with that type, until it happens to change again. This results in a subtle loss of information, rather than outright failure. Like I said, programming with combinators is a great way to reuse code -- but not all combinators that are of type "f a -> (a -> g b) -> g b" are monadic. It's only a monad when f = g. |
|
For example, I see you are a Perl hacker. In Perl, even my simple (42 + "foo") example results in "subtle loss of information." This doesn't mean that + doesn't work in Perl, just that someone other than the compiler has to make sure the values on either side have compatible types.
Look at the clojure.contrib.monad documentation, and you'll see that monads do have benefits beyond "normal" combinators, even if those benefits don't include all the static safety of Haskell or ML. You get the "domonad" macro which provides the same benefits as Haskell's "do" syntactic sugar. You get generic lifting/mapping/chaining functions that you can use with any monad (like the Control.Monad module in Haskell), and you can write similar generic functions of your own:
http://richhickey.github.com/clojure-contrib/monads-api.html
Generic/parametric monad actions do look different in a language with a weak type system. Rather than letting type inference take care of polymorphism for you, you have to use other language features. For example, in JavaScript you can use objects and duck-typing, letting the function access the enclosing monad's operations through an implicit object parameter. I don't know enough Clojure yet to know the equivalent idiom.