Created
August 29, 2011 17:36
-
-
Save qzchenwl/1178905 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- file: ch15/Supply.hs | |
next = S $ do st <- get | |
case st of | |
[] -> return Nothing | |
(x:xs) -> do put xs | |
return (Just x) |
@md2perpe This is an example from Real World Haskell.
The get
is defined as:
get :: State s s
get = State $ \s -> (s, s)
The (>>=)
is defined as:
bindState :: State s a -> (a -> State s b) -> State s b
bindState m k = State $ \s -> let (a, s') = runState m s
in runState (k a) s'
We can apply function (\s -> let (a, s') = runState m s ...)
on st
, so st
must have type of s
for State s a
In the Supply example, State [a] (Maybe a)
... I think I know...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The "method"
get
returns the state used in the state monad. That will be[a]
because you matchst
against[]
and(x:xs)
. The fulldo
expression will however beState [a](Maybe a)
since you returnJust x
which is of typeMaybe a
.