haskell4.hs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <<<<<<< HEAD
  2. lenght :: [a] -> Integer
  3. lenght [] = 0
  4. lenght (x:xs) = 1 + lenght xs
  5. data Tree a = Leaf a | Branch (Tree a) (Tree a)
  6. aTree = Branch (Leaf 'a') (Branch (Leaf 'b') (Leaf 'c'))
  7. ones = 1 : ones
  8. numsFrom n = n : numsFrom (n + 1)
  9. fib = 1 : 1 : [a + b | (a, b) <- zip fib (tail fib)]
  10. data TrafficLight = Red | Yellow | Green
  11. instance Eq TrafficLight where
  12. Red == Red = True
  13. Yellow == Yellow = True
  14. Green == Green = True
  15. _ == _ = False
  16. instance Show TrafficLight where
  17. show Red = "The color of the semaphore is Red"
  18. show Yellow = "The color of the semaphore is Yellow"
  19. show Green = "The color of the semaphore is Green"
  20. data CMaybe a = CNothing | CJust Int a deriving (Show)
  21. instance Functor CMaybe where
  22. fmap f CNothing = CNothing
  23. fmap f (CJust counter x) = CJust (counter+1) (f x)
  24. applyMaybe :: Maybe a -> (a -> Maybe b) -> Maybe b
  25. applyMaybe Nothing _ = Nothing
  26. applyMaybe (Just x) f = f x
  27. data Clist a = CNode a (Clist a) | CEnd (Clist a)
  28. instance (Eq a) => Eq (Clist a) where
  29. CEnd _ == CEnd _ = True
  30. (CNode a next) == (CNode b next2) = (a==b) && next == next2
  31. _ == _ = False
  32. clist2list :: Clist a -> [a]
  33. clist2list (CEnd end) = []
  34. clist2list (CNode a next) = a : clist2list next
  35. list2clist :: [a] -> Clist a
  36. list2clist [] = let new = CEnd new
  37. in new
  38. list2clist (x:xs) = let first = CNode x $ list2clist' xs first
  39. in first
  40. list2clist' [] first = CEnd first
  41. list2clist' (x:xs) first = CNode x $ list2clist' xs first
  42. cmap :: (a -> b) -> Clist a -> Clist b
  43. cmap f (CNode x next) = let first = CNode (f x) $ cmap' f next first
  44. in first
  45. cmap' f (CEnd x) first = (CEnd first)
  46. cmap' f (CNode x next) first = CNode (f x) $ cmap' f next first
  47. transpose :: [[a]] -> [[a]]
  48. transpose [] = []
  49. transpose ls = let hs = map head ls
  50. ts = filter (not . null) $ map tail ls
  51. in hs : transpose ts
  52. =======
  53. module Haskell4 where
  54. import Control.Applicative
  55. import Control.Monad
  56. exmon :: (Monad m, Num r) => m r -> m r -> m r
  57. exmon m1 m2 = do x <- m1
  58. y <- m2
  59. return $ x-y
  60. main = exmon (do putStr "?> "
  61. x <- getLine;
  62. return (read x :: Int))
  63. (return 10)
  64. >>>>>>> 66d23fcd2717ce0805f5442c6cf4888114f9b38c