State Monad, sequences of random numbers and monadic code

前端 未结 3 1401
礼貌的吻别
礼貌的吻别 2020-12-28 08:46

I\'m trying to grasp the State Monad and with this purpose I wanted to write a monadic code that would generate a sequence of random numbers using a Linear Congruential Gene

3条回答
  •  梦谈多话
    2020-12-28 09:13

    You've got a couple great responses. What I do when working with the State monad is in my mind replace State s a with s -> (s,a) (after all, that's really what it is).

    You then get a type for bind that looks like:

    (>>=) :: (s -> (s,a)) ->
             (a -> s -> (s,b)) ->
             (s -> (s,b))
    

    and you see that bind is just a specialized kind of function composition operator, like (.)

    I wrote a blog/tutorial on the state monad here. It's probably not particularly good, but helped me grok things a little better by writing it.

提交回复
热议问题