haskell7.hs 840 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import Control.Monad.State
  2. type Stack = [Int]
  3. --Define the pop function
  4. pop :: Stack -> (Int, Stack)
  5. pop (x:xs) = (x, xs)
  6. --Define the push function
  7. push :: Int -> Stack -> ((), Stack)
  8. push x xs = ((), x:xs)
  9. stackManip :: Stack -> (Int, Stack)
  10. stackManip stack = let
  11. (a, stack1) = pop stack
  12. (b, stack2) = pop stack1
  13. ((), stack3) = push 100 stack2
  14. (c, stack4) = pop stack3
  15. in pop stack4
  16. popM :: State Stack Int
  17. popM = do
  18. x:xs <- get
  19. put xs
  20. return x
  21. pushM :: Int -> State Stack ()
  22. pushM a = do
  23. xs <- get
  24. put (a:xs)
  25. return()
  26. stackManipM :: State Stack Int
  27. stackManipM = do
  28. popM
  29. popM
  30. pushM 100
  31. popM
  32. popM
  33. stackStuff :: State Stack ()
  34. stackStuff = do
  35. a <- popM
  36. if (a==5)
  37. then return ()
  38. else do
  39. pushM 3
  40. pushM 8
  41. get