Hacker News new | ask | show | jobs
by yen223 2958 days ago
Haskell-style types aren't a prerequisite for functional programming. Lisp and Elixir are dynamically-typed, which is why you don't see monads in those.

That said, they do occasionally form useful abstractions.

2 comments

> Haskell-style types aren't a prerequisite for functional programming.

Sure.

> Lisp and Elixir are dynamically-typed, which is why you don't see monads in those.

More to the point, Lisp and Elixir are impure functional languages, which is why you don't have some pure construct, like monads, that isolates IO.

> you don't have some pure construct, like monads, that isolates IO

In Haskell IO is isolated, and then the Monad interface is used for some particularly important ways of interacting with values of that type. That's not quite the same thing as "monads isolate IO" - the Monad interface is useful for quite a few other types of values.

Can you explain why you don't need monads if your language is dynamically typed? That's a new one to me.
I don't agree with that "not needed" explanation. But they are quite useless if the compiler can not deduce that both the values returned from the functions you are calling are monads, and what kind of binding should apply to them.
I find them to be at least somewhat useful in a language like javascript. I imagine they are more useful in a typed language with proper inference, but I wouldn't say they're entirely useless in a dynamic language.
OK, for the slow of understanding:

What I think you said is that, in an untyped language, a function will just return "something" (as far as the compiler is concerned). But in a typed language, the compiler knows that a monad was returned. So the point of the monad is to make the compiler do magic, not to make the runtime do magic.

Is that accurate?

Well.. It's like 25% accurate, and you hit the part you were most worried about. But you have been seriously misled about monads in the past.

There is no magic in monads. At the level of runtime operations, they're just a couple of functions.

The magic taking place in the compiler is type inference guiding the selection of the right pair of functions during compilation. Without that, you have to explicitly use the correct functions. It's much easier to offload that bookkeeping to a computer than it is to do it yourself.