haskell7.hs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <<<<<<< HEAD
  2. import Control.Monad.State
  3. type Stack = [Int]
  4. --Define the pop function
  5. pop :: Stack -> (Int, Stack)
  6. pop (x:xs) = (x, xs)
  7. --Define the push function
  8. push :: Int -> Stack -> ((), Stack)
  9. push x xs = ((), x:xs)
  10. stackManip :: Stack -> (Int, Stack)
  11. stackManip stack = let
  12. (a, stack1) = pop stack
  13. (b, stack2) = pop stack1
  14. ((), stack3) = push 100 stack2
  15. (c, stack4) = pop stack3
  16. in pop stack4
  17. popM :: State Stack Int
  18. popM = do
  19. x:xs <- get
  20. put xs
  21. return x
  22. pushM :: Int -> State Stack ()
  23. pushM a = do
  24. xs <- get
  25. put (a:xs)
  26. return()
  27. stackManipM :: State Stack Int
  28. stackManipM = do
  29. popM
  30. popM
  31. pushM 100
  32. popM
  33. popM
  34. stackStuff :: State Stack ()
  35. stackStuff = do
  36. a <- popM
  37. if (a==5)
  38. then return ()
  39. else do
  40. pushM 3
  41. pushM 8
  42. get
  43. =======
  44. --Define the Bilist type
  45. data Bilist a = Bilist [a] [a] deriving (Show, Eq)
  46. bilist_ref (Bilist l r) pos = (l !! pos, r !! pos)
  47. --Define oddeven
  48. oddeven :: [a] -> Bilist a
  49. oddeven l = oddevenh l [] []
  50. oddevenh :: [a] -> [a] -> [a] -> Bilist a
  51. oddevenh [] ev od = Bilist ev od
  52. oddevenh (x:xs) ev od = oddevenh xs od (ev++[x])
  53. --Define the inverse of oddeven
  54. inv_oddeven :: Bilist a -> [a]
  55. inv_oddeven (Bilist [] []) = []
  56. inv_oddeven (Bilist (l:ls) (r:rs)) = [l] ++ [r] ++ (inv_oddeven (Bilist ls rs))
  57. inv_oddeven2 :: Bilist a -> [a]
  58. inv_oddeven2 (Bilist l r) = foldl (++) [] $ map (\(x,y) -> [x,y]) $ zip l r
  59. bilist_max (Bilist (l:ls) (r:rs)) = bilist_maxh (Bilist ls rs) 2 (l+r) 1
  60. bilist_maxh (Bilist (l:ls) (r:rs)) pos curmax maxpos |
  61. l+r > curmax = bilist_maxh (Bilist ls rs) (pos+1) (l+r) pos
  62. bilist_maxh (Bilist (l:ls) (r:rs)) pos curmax maxpos =
  63. bilist_maxh (Bilist ls rs) (pos+1) curmax maxpos
  64. bilist_maxh (Bilist [] []) pos curmax maxpos = maxpos
  65. >>>>>>> 66d23fcd2717ce0805f5442c6cf4888114f9b38c