Why monads? How does it resolve side-effects?

后端 未结 7 895
感情败类
感情败类 2020-12-22 23:23

I am learning Haskell and trying to understand Monads. I have two questions:

  1. From what I understand, Monad is just another typeclass that declares ways to i

7条回答
  •  旧巷少年郎
    2020-12-22 23:51

    the point is so there can be clean error handling in a chain of functions, containers, and side effects

    More or less.

    how exactly is the problem of side-effects solved?

    A value in the I/O monad, i.e. one of type IO a, should be interpreted as a program. p >> q on IO values can then be interpreted as the operator that combines two programs into one that first executes p, then q. The other monad operators have similar interpretations. By assigning a program to the name main, you declare to the compiler that that is the program that has to be executed by its output object code.

    As for the list monad, it's not really related to the I/O monad except in a very abstract mathematical sense. The IO monad gives deterministic computation with side effects, while the list monad gives non-deterministic (but not random!) backtracking search, somewhat similar to Prolog's modus operandi.

提交回复
热议问题