1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- module Haskell6 where
- import Control.Monad.ST
- --State Monad
- ----Stack
- type Stack = [Int]
- --define the pop function
- pop :: Stack -> (Int,Stack)
- pop (x:xs) = (x,xs)
- --define the push function
- push :: Int -> Stack -> ((),Stack)
- push a xs = ((), a:xs)
- --Define Stackmanipulator
- stackManip :: Stack -> (Int, Stack)
- stackManip stack = let
- (a, newstack1) = pop stack
- (b, newstack2) = pop newstack1
- ((), newstack3) = push 100 newstack2
- (c, newstack4) = pop newstack3
- in pop newstack4
- --Define the pop function using the state monad
- popM :: State Stack Int
- popM = do
- x:xs <- get
- put xs
- return x
- --Define the push function using the state monad
- pushM :: Int -> State Stack ()
- push a = do
- xs <- get
- put (a:xs)
- return ()
- --Redefine the stackManip using the monadic functions
- stackManipM :: Stack State Int
- stackManipM = do
- popM
- popM
- pushM 100
- popM
- popM
|