How to implement a global counter using Monad?

前端 未结 4 1225
[愿得一人]
[愿得一人] 2020-12-30 07:09

I need a global counter, starting from 0, 1, 2, 3, ..... I kind of understand that this \"impure\" code should be separately implemented... I am just starting to understand

4条回答
  •  攒了一身酷
    2020-12-30 07:45

    While State is fine, you don't need to inspect the counter while calculating, but just to increase it, so the Writer monad should be sufficient. See Learn you a Haskell for a (not too serious) introduction.

提交回复
热议问题