haskell6.hs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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