haskell6.hs 974 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. module Haskell6 where
  2. import Control.Monad.ST
  3. --State Monad
  4. ----Stack
  5. type Stack = [Int]
  6. --define the pop function
  7. pop :: Stack -> (Int,Stack)
  8. pop (x:xs) = (x,xs)
  9. --define the push function
  10. push :: Int -> Stack -> ((),Stack)
  11. push a xs = ((), a:xs)
  12. --Define Stackmanipulator
  13. stackManip :: Stack -> (Int, Stack)
  14. stackManip stack = let
  15. (a, newstack1) = pop stack
  16. (b, newstack2) = pop newstack1
  17. ((), newstack3) = push 100 newstack2
  18. (c, newstack4) = pop newstack3
  19. in pop newstack4
  20. --Define the pop function using the state monad
  21. popM :: State Stack Int
  22. popM = do
  23. x:xs <- get
  24. put xs
  25. return x
  26. --Define the push function using the state monad
  27. pushM :: Int -> State Stack ()
  28. push a = do
  29. xs <- get
  30. put (a:xs)
  31. return ()
  32. --Redefine the stackManip using the monadic functions
  33. stackManipM :: Stack State Int
  34. stackManipM = do
  35. popM
  36. popM
  37. pushM 100
  38. popM
  39. popM