State Monad

Log in om je oplossingen te testen.
import Control.Monad (liftM, ap) data State s a = State (s -> (a,s)) instance Monad (State s) where return a = State $ \s -> (a,s) m >>= k = State $ \s -> let (x,s') = run m s in run (k x) s' instance Functor (State s) where -- ... instance Applicative (State s) where -- ... -- Run the state transformer on given state, return the a value and the resulting state. run :: State s a -> s -> (a, s) run (State f) s = undefined -- Get the state. get :: State s s get = undefined -- Set the state. put :: s -> State s () put s = undefined -- Modify the state. modify :: (s -> s) -> State s () modify f = undefined -- Implement stack operations as state transformations. type Stack = [Int] push :: Int -> State Stack () push x = undefined pop :: State Stack Int pop = undefined add, mult :: State Stack () add = undefined mult = undefined
Je kunt zo vaak indienen als je wenst. Er wordt enkel rekening gehouden met je laatst ingediende oplossing.
Log in om je oplossingen te testen.