How Monads are considered Pure?

前端 未结 4 1101
名媛妹妹
名媛妹妹 2020-12-09 13:23

I am very much new to Haskell, and really impressed by the language\'s \"architecture\", but it still bothers me how monads can be pure.

As

4条回答
  •  孤街浪徒
    2020-12-09 13:51

    One way to think of this is that a value of type IO a is a "recipe", containing a list of instructions that if performed would have side effects. Constructing that "recipe" though, does not have any side effects. So a haskell program (whose type is IO ()) is basically a computation that constructs such a recipe. Importantly, the program does not execute any of the instructions in the recipe. When the recipe is completed the program terminates. Then the compiler (or interpreter) takes that recipe and executes it. But the code that the programmer wrote is not running anymore, so the instructions in the recipe are executed outside the scope of the program.

提交回复
热议问题