haskell4.hs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. lenght :: [a] -> Integer
  2. lenght [] = 0
  3. lenght (x:xs) = 1 + lenght xs
  4. data Tree a = Leaf a | Branch (Tree a) (Tree a)
  5. aTree = Branch (Leaf 'a') (Branch (Leaf 'b') (Leaf 'c'))
  6. ones = 1 : ones
  7. numsFrom n = n : numsFrom (n + 1)
  8. fib = 1 : 1 : [a + b | (a, b) <- zip fib (tail fib)]
  9. data TrafficLight = Red | Yellow | Green
  10. instance Eq TrafficLight where
  11. Red == Red = True
  12. Yellow == Yellow = True
  13. Green == Green = True
  14. _ == _ = False
  15. instance Show TrafficLight where
  16. show Red = "The color of the semaphore is Red"
  17. show Yellow = "The color of the semaphore is Yellow"
  18. show Green = "The color of the semaphore is Green"
  19. data CMaybe a = CNothing | CJust Int a deriving (Show)
  20. instance Functor CMaybe where
  21. fmap f CNothing = CNothing
  22. fmap f (CJust counter x) = CJust (counter+1) (f x)
  23. applyMaybe :: Maybe a -> (a -> Maybe b) -> Maybe b
  24. applyMaybe Nothing _ = Nothing
  25. applyMaybe (Just x) f = f x
  26. data Clist a = CNode a (Clist a) | CEnd (Clist a)
  27. instance (Eq a) => Eq (Clist a) where
  28. CEnd _ == CEnd _ = True
  29. (CNode a next) == (CNode b next2) = (a==b) && next == next2
  30. _ == _ = False
  31. clist2list :: Clist a -> [a]
  32. clist2list (CEnd end) = []
  33. clist2list (CNode a next) = a : clist2list next
  34. list2clist :: [a] -> Clist a
  35. list2clist [] = let new = CEnd new
  36. in new
  37. list2clist (x:xs) = let first = CNode x $ list2clist' xs first
  38. in first
  39. list2clist' [] first = CEnd first
  40. list2clist' (x:xs) first = CNode x $ list2clist' xs first
  41. cmap :: (a -> b) -> Clist a -> Clist b
  42. cmap f (CNode x next) = let first = CNode (f x) $ cmap' f next first
  43. in first
  44. cmap' f (CEnd x) first = (CEnd first)
  45. cmap' f (CNode x next) first = CNode (f x) $ cmap' f next first
  46. transpose :: [[a]] -> [[a]]
  47. transpose [] = []
  48. transpose ls = let hs = map head ls
  49. ts = filter (not . null) $ map tail ls
  50. in hs : transpose ts