Advertisement
KillianMills

exercise4.hs

Oct 23rd, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. myAppend :: [a] -> [a] -> [a]
  2. myAppend [] x = x
  3. myAppend (x:xs) y = x : myAppend xs y
  4.  
  5. myHead :: [a] -> a
  6. myHead [] = error "Idiot alert, empty list"
  7. myHead (x:_) = x
  8.  
  9. myLast :: [a] -> a
  10. myLast [] = error "Idiot alert, empty list"
  11. myLast [x] = x
  12. myLast (_:xs) = myLast xs
  13.  
  14. myInit :: [a] -> [a]
  15. myInit [] = error "Idiot alert, empty list"
  16. myInit [x] = []
  17. myInit (x:y:[]) = [x]
  18. myInit (x:xs) = x : myInit(xs)
  19.  
  20. myLength :: [a] -> Int
  21. myLength [] = 0
  22. myLength (x:xs) = 1 + myLength xs
  23.  
  24. myReverse :: [a] -> [a]
  25. myReverse [] = []
  26. myReverse (x:xs) = myReverse xs ++ [x]
  27.  
  28. myConcat :: [[a]] -> [a]
  29. myConcat [] = []
  30. myConcat (x:xs) = x ++ myConcat xs
  31.  
  32. mySum :: Num a => [a] -> a
  33. mySum [] = 0
  34. mySum (x:xs) = x + mySum(xs)
  35.  
  36. myProduct :: Num a => [a] -> a
  37. myProduct [] = error "Idiot alert, empty list"
  38. myProduct [x] = x
  39. myProduct (x:xs) = x * myProduct(xs)
  40.  
  41. myMaximum :: Ord a => [a] -> a
  42. myMaximum [] = error "Idiot alert, empty list"
  43. myMaximum [x] = x
  44. myMaximum (x:xs) = max x (myMaximum xs)
  45.  
  46. myMinimum :: Ord a => [a] -> a
  47. myMinimum [] = error "Idiot alert, empty list"
  48. myMinimum [x] = x
  49. myMinimum (x:xs) = min x (myMinimum xs)
  50.  
  51. myElem :: Eq a => a -> [a] -> Bool
  52. myElem x [] = False
  53. myElem x (y:ys)
  54.     | x == y = True
  55.     | otherwise = myElem x ys
  56.  
  57. myDelete :: Eq a => a -> [a] -> [a]
  58. myDelete _ [] = []
  59. myDelete a (x:xs)
  60.     | a == x = xs
  61.     | otherwise = x  : (myDelete a xs)
  62.  
  63. myIntersect :: Eq a => [a] -> [a] -> [a]
  64. myIntersect _ [] = []
  65. myIntersect [] _ = []
  66. myIntersect (x:xs) ys
  67.     | myElem x ys = x : myIntersect xs ys
  68.     | otherwise = myIntersect xs ys
  69.    
  70.    
  71.    
  72.    
  73. --myAppend :: [a] -> [a] -> [a]
  74. --myAppend [] [] = error "Idiot alert, empty lists"
  75. --myAppend a b = a ++ b
  76.                
  77.                
  78. --myHead :: [a] -> a
  79. --myHead [] = error "Idiot alert, empty list"
  80. --myHead (a:_) = a
  81.  
  82. --myLast ::(Eq a) => [a] -> a
  83. --myLast [] = error "Idiot alert, empty list"
  84. --myLast (x:xs) = if xs ==[]
  85.     --          then x
  86.     --          else myLast(xs)
  87.                
  88. --myTail ::(Eq a) => [a] -> a
  89. --myTail [] = error "Idiot alert, empty list"
  90. --myTail (x:xs) = if xs ==[]
  91.     --          then x
  92.     --          else xs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement