2

I have been looking at monads a lot over the past few months (functors and applicative functors as well). I have been attempting to figure out when monads are useful in a general sense. If I am looking at a piece of code I ask, should I employ a specific monad or a stack via transformers? In my efforts I think I have found an answer but I want others input in case I have missed something. It appears to me that monads are useful for abstracting away specific plumbing to increase readabilty/the declaritive nature of a piece of code which can have a side affect of increasing productivity by requiring less code to write. The only exception I can find is the IO monad which attempts to keep a pure function pure in the face of IO. It doesn't appear that a given monad provides a solution to a problem that can't be acheived via other means. Am I missing something?

Brad
  • 705
  • 7
  • 17
  • Isn't this true of almost all language constructs? You can write every program using the four simple instructions of a Universal Register Machine, but you wouldn't want to. – arx Dec 27 '11 at 19:27
  • Related: https://stackoverflow.com/questions/28139259/why-do-we-need-monads . – atravers Jan 13 '22 at 01:56

1 Answers1

4

Does any feature beyond mere Turing-completeness provide a solution to a problem that can't be achieved via other means? Nope. All Turing-equivalent languages are different ways of expressing the same basic things. Since monads are built out of more fundamental building blocks, obviously that set of building blocks is able to do anything a monad can. When we talk about a language or feature "allowing" us to do something, we mean it allows us to express that thing naturally, in a way that's easy to understand.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • 1
    So then a more appropriate question (more so to ask myself, not SO) is when does a given monad's benefits outweigh it's cost or even more general what is the opportunity cost of a given piece of code. By writing it one way, what other ways of writing it am I missing out on, good or bad. hmmmm – Brad Dec 27 '11 at 19:42