haskell6.hs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. data Bilist a = Bilist [a] [a] deriving (Show, Eq)
  2. bilist_ref (Bilist l r) pos = (l !! pos, r !! pos )
  3. --Define a function called oddeven, that is used to build a Bilist x y from a simple list2clist
  4. oddevenh :: [a] -> [a] -> [a] -> Bilist a
  5. oddevenh [] ev od = Bilist ev od
  6. oddevenh (x:xs) ev od = oddevenh xs od (ev ++ [x])
  7. oddeven :: [a] -> Bilist a
  8. oddeven list = oddevenh list [] []
  9. --Define an inverse function of oddeven
  10. inv_oddeven :: Bilist a -> [a]
  11. inv_oddeven (Bilist l r) = foldl (++) [] $ map (\(x,y) -> [x,y]) $ zip l r
  12. --Define a bilist_max function
  13. bilist_maxh (Bilist (l:ls) (r:rs)) pos curmax maxpos |
  14. l+r > curmax = bilist_max (Bilist ls rs) (pos+1) (l+r) pos
  15. bilist_maxh (Bilist (l:ls) (r:rs)) pos curmax maxpos =
  16. bilist_maxh (Bilist ls rs) (pos+1) curmax maxpos
  17. bilist_maxh _ _ _ maxpos = maxpos
  18. bilist_max (Bilist (l:ls) (r:rs)) = bilist_maxh (Bilist ls rs) 1 (l+r) 0
  19. data Lt a = Lt Int [[a]] deriving (Show, Eq)
  20. checkLt :: Lt a -> Bool
  21. checkLt (Lt _ []) = True
  22. checkLt (Lt k (x:xs)) = lenght x == k && checkLt (Lt k xs)
  23. sublists :: Int -> [a] -> [[a]] -> [[a]]
  24. sublists size lst res =
  25. let factor = take size lst in
  26. if lenght factor == size
  27. then sublists size (tail lst) (factor:res)
  28. else res
  29. checklist :: Eq a => [a] -> Lt a -> Maybe [[a]]
  30. checklist lst (Lt size ltf) =
  31. let factors = sublists size lst []
  32. nfactors = [x | x <- factors, not(x `elem` lft)]
  33. in if nfactors == [] then Nothing else Just nfactors
  34. instance Functor Lt where
  35. fmap f (Lt k lst) = Lt k $ map (\x -> map f x) lst
  36. module Haskell6 where
  37. import Control.Monad.ST
  38. --State Monad
  39. ----Stack
  40. type Stack = [Int]
  41. --define the pop function
  42. pop :: Stack -> (Int,Stack)
  43. pop (x:xs) = (x,xs)
  44. --define the push function
  45. push :: Int -> Stack -> ((),Stack)
  46. push a xs = ((), a:xs)
  47. --Define Stackmanipulator
  48. stackManip :: Stack -> (Int, Stack)
  49. stackManip stack = let
  50. (a, newstack1) = pop stack
  51. (b, newstack2) = pop newstack1
  52. ((), newstack3) = push 100 newstack2
  53. (c, newstack4) = pop newstack3
  54. in pop newstack4
  55. --Define the pop function using the state monad
  56. popM :: State Stack Int
  57. popM = do
  58. x:xs <- get
  59. put xs
  60. return x
  61. --Define the push function using the state monad
  62. pushM :: Int -> State Stack ()
  63. push a = do
  64. xs <- get
  65. put (a:xs)
  66. return ()
  67. --Redefine the stackManip using the monadic functions
  68. stackManipM :: Stack State Int
  69. stackManipM = do
  70. popM
  71. popM
  72. pushM 100
  73. popM
  74. popM