haskell7.hs 978 B

123456789101112131415161718192021222324252627282930
  1. --Define the Bilist type
  2. data Bilist a = Bilist [a] [a] deriving (Show, Eq)
  3. bilist_ref (Bilist l r) pos = (l !! pos, r !! pos)
  4. --Define oddeven
  5. oddeven :: [a] -> Bilist a
  6. oddeven l = oddevenh l [] []
  7. oddevenh :: [a] -> [a] -> [a] -> Bilist a
  8. oddevenh [] ev od = Bilist ev od
  9. oddevenh (x:xs) ev od = oddevenh xs od (ev++[x])
  10. --Define the inverse of oddeven
  11. inv_oddeven :: Bilist a -> [a]
  12. inv_oddeven (Bilist [] []) = []
  13. inv_oddeven (Bilist (l:ls) (r:rs)) = [l] ++ [r] ++ (inv_oddeven (Bilist ls rs))
  14. inv_oddeven2 :: Bilist a -> [a]
  15. inv_oddeven2 (Bilist l r) = foldl (++) [] $ map (\(x,y) -> [x,y]) $ zip l r
  16. bilist_max (Bilist (l:ls) (r:rs)) = bilist_maxh (Bilist ls rs) 2 (l+r) 1
  17. bilist_maxh (Bilist (l:ls) (r:rs)) pos curmax maxpos |
  18. l+r > curmax = bilist_maxh (Bilist ls rs) (pos+1) (l+r) pos
  19. bilist_maxh (Bilist (l:ls) (r:rs)) pos curmax maxpos =
  20. bilist_maxh (Bilist ls rs) (pos+1) curmax maxpos
  21. bilist_maxh (Bilist [] []) pos curmax maxpos = maxpos