0

Let's take the simple case here (pseudo code)

def readnonfp(): String = {
    nonMonadicIO.readFile("somefile")
}

def readfp(): IO[String] = {
    monadicIO.readFile("somefile")
}

Now this will be maybe composed like this

length(readnonfp())
readnonfp >>= length

What exactly is the difference between the two ? What is IO monad good at than it's counterpart way of doing IO?

ffff
  • 2,853
  • 1
  • 25
  • 44
  • Do you know what a monad is/does? – Willem Van Onsem Aug 07 '17 at 17:08
  • @WillemVanOnsem i know the construct, still understanding more about it – ffff Aug 07 '17 at 17:09
  • It tells you what `readfp` is computing by looking at the type alone. And being able to typecheck that it's doing what you want. – Bergi Aug 07 '17 at 17:34
  • Note that monadic IO (or some other contrivance) is only necessary in *lazy* languages, most "fp" languages don't use monadic IO (ocaml, clojure, erlang, etc). – Jared Smith Aug 07 '17 at 18:36
  • 2
    @JaredSmith Even for lazy languages there are other ways of doing IO than a monad data type. – Bergi Aug 07 '17 at 21:41
  • Agreed with Bergi. @JaredSmith Monadic IO is used in some lazy languages for three reasons. It is nicer than the alternatives and it provides an abstraction that is useful for other things. It also makes IO actions first-class in the sense that you can combine them, pass them around (and when they are passed to a function it could decide to combine them, ignore them, repeat them, pass them to other functions, etc. All of those things potentially predicated on some conditionals), etc. – David Young Aug 08 '17 at 01:33
  • relevant: [*IO Monad Considered Harmful*](https://blog.jle.im/entry/io-monad-considered-harmful.html) – Mulan Aug 09 '17 at 06:18

0 Answers0