I am trying to do a global counter using monads in Haskell, I want to get the incremented value every time I use the monad counter, but I am kind of stuck getting the same value each time! The code list is as follows:
module CounterMonad where
data Counter a = C (Int -> (Int, a))
--reset the counter
new :: Counter ()
new = C $ \_ -> (0, ())
-- increment the counter:
--inc :: Counter Int
--inc = C $ \n -> (n+1, n)
inc = get >>= \s -> (put (s+1))
-- returning the current value of the counter
get :: Counter Int
get = C $ \n -> (n, n)
--
put x = C $ \n -> (x, x)
--return is nop, >>= is sequential exectuion
instance Monad Counter where
return r = C $ \n -> (n, r)
(>>=) (C f) g = C $ \n0 -> let (n1, r1) = f n0
C g' = g r1
in g' n1
run :: Counter a -> a
run (C f) = snd (f 0)
tickC = do
inc
c <- get
return c
When I try to execute as run tickC
, it always returns 1.
What I want is everytime I run tickC
, it returns the incremented value, like 1, ,2, 3,4 ....
I know there must be some stupid problem lying there, can you guys point out how?