Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Informatics 1 - Functional Programming
  2. -- Tutorial 3
  3. --
  4. -- Week 5 - Due: 19-21 Oct.
  5.  
  6. import Data.Char
  7. import Data.List
  8. import Test.QuickCheck
  9.  
  10.  
  11.  
  12. -- 1. Map
  13. -- a.
  14. uppers :: String -> String
  15. uppers = map toUpper
  16.  
  17. -- b.
  18. doubles :: [Int] -> [Int]
  19. doubles = map (*2)
  20.  
  21. -- c.
  22. penceToPounds :: [Int] -> [Float]
  23. penceToPounds = map ((/100) . fromIntegral)
  24.  
  25. -- d.
  26. uppers' :: String -> String
  27. uppers' xs = [toUpper x | x <- xs]
  28.  
  29. prop_uppers :: String -> Bool
  30. prop_uppers xs = uppers xs == uppers' xs
  31.  
  32.  
  33.  
  34. -- 2. Filter
  35. -- a.
  36. alphas :: String -> String
  37. alphas = filter isAlpha
  38.  
  39. -- b.
  40. rmChar ::  Char -> String -> String
  41. rmChar = filter . (/=)
  42.  
  43. -- c.
  44. above :: Int -> [Int] -> [Int]
  45. above = filter . flip (>)
  46.  
  47. -- d.
  48. unequals :: [(Int,Int)] -> [(Int,Int)]
  49. unequals = filter (\(x,y) -> x == y)
  50.  
  51. -- e.
  52. rmCharComp :: Char -> String -> String
  53. rmCharComp ch xs = [x | x <- xs, x /= ch]
  54.  
  55. prop_rmChar :: Char -> String -> Bool
  56. prop_rmChar ch xs = rmChar ch xs == rmCharComp ch xs
  57.  
  58.  
  59.  
  60. -- 3. Comprehensions vs. map & filter
  61. -- a.
  62. upperChars :: String -> String
  63. upperChars s = [toUpper c | c <- s, isAlpha c]
  64.  
  65. upperChars' :: String -> String
  66. upperChars' = map toUpper . filter isAlpha
  67.  
  68. prop_upperChars :: String -> Bool
  69. prop_upperChars s = upperChars s == upperChars' s
  70.  
  71. -- b.
  72. largeDoubles :: [Int] -> [Int]
  73. largeDoubles xs = [2 * x | x <- xs, x > 3]
  74.  
  75. largeDoubles' :: [Int] -> [Int]
  76. largeDoubles' = map (*2) . filter (>3)
  77.  
  78. prop_largeDoubles :: [Int] -> Bool
  79. prop_largeDoubles xs = largeDoubles xs == largeDoubles' xs
  80.  
  81. -- c.
  82. reverseEven :: [String] -> [String]
  83. reverseEven strs = [reverse s | s <- strs, even (length s)]
  84.  
  85. reverseEven' :: [String] -> [String]
  86. reverseEven' = map reverse . filter (even . length)
  87.  
  88. prop_reverseEven :: [String] -> Bool
  89. prop_reverseEven strs = reverseEven strs == reverseEven' strs
  90.  
  91.  
  92.  
  93. -- 4. Foldr
  94. -- a.
  95. productRec :: [Int] -> Int
  96. productRec []     = 1
  97. productRec (x:xs) = x * productRec xs
  98.  
  99. productFold :: [Int] -> Int
  100. productFold = foldr (*) 1
  101.  
  102. prop_product :: [Int] -> Bool
  103. prop_product xs = productRec xs == productFold xs
  104.  
  105. -- b.
  106. andRec :: [Bool] -> Bool
  107. andRec [] = True
  108. andRec (x:xs) | x == True = andRec xs
  109.               | otherwise = False
  110.  
  111. andFold :: [Bool] -> Bool
  112. andFold = foldr (&&) True
  113.  
  114. prop_and :: [Bool] -> Bool
  115. prop_and xs = andRec xs == andFold xs
  116.  
  117. -- c.
  118. concatRec :: [[a]] -> [a]
  119. concatRec [] = []
  120. concatRec (x:xs) = x ++ concatRec xs
  121.  
  122. concatFold :: [[a]] -> [a]
  123. concatFold = foldr (++) []
  124.  
  125. prop_concat :: [String] -> Bool
  126. prop_concat strs = concatRec strs == concatFold strs
  127.  
  128. -- d.
  129. rmCharsRec :: String -> String -> String
  130. rmCharsRec [] ys = ys
  131. rmCharsRec (x:xs) ys = rmCharsRec xs $ rmChar x ys
  132.  
  133. rmCharsFold :: String -> String -> String
  134. rmCharsFold = flip (foldr rmChar)
  135.  
  136. prop_rmChars :: String -> String -> Bool
  137. prop_rmChars chars str = rmCharsRec chars str == rmCharsFold chars str
  138.  
  139.  
  140.  
  141. type Matrix = [[Int]]
  142.  
  143.  
  144. -- 5
  145. -- a.
  146. uniform :: [Int] -> Bool
  147. uniform [] = True
  148. uniform (x:xs) = all (==x) xs
  149.  
  150. all' :: (a -> Bool) -> [a] -> Bool
  151. all' f = foldr (&&) True . map f
  152.  
  153. uniform' :: [Int]  -> Bool
  154. uniform' [] = True
  155. uniform' (x:xs) = all' (==x) xs
  156.  
  157. prop_uniform :: [Int] -> Bool
  158. prop_uniform xs = uniform xs == uniform' xs
  159.  
  160. -- b.
  161. valid :: Matrix -> Bool
  162. valid (x:xs) = all' (\a -> length a == length x) xs
  163.  
  164. -- 6.
  165.  
  166. -- a: 18, converts a curried function into a function on pairs
  167. -- b.
  168. zipWith'' :: (a -> b -> c) -> [a] -> [b] -> [c]
  169. zipWith'' f xs ys = [f x y | (x,y) <- zip xs ys]
  170. -- c.
  171. zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
  172. zipWith' f xs ys = map (uncurry f) $ zip xs ys
  173.  
  174. -- 7.
  175. plusM :: Matrix -> Matrix -> Matrix
  176. plusM = zipWith (zipWith (+))
  177.  
  178. -- 8. -- STUFF FROM HERE ON IS BAD
  179. timesM :: Matrix -> Matrix -> Matrix
  180. timesM a b | validIn a b = multiplyM a (transpose' b)
  181.           | otherwise = error "Invalid input"
  182.  
  183. validIn :: Matrix -> Matrix -> Bool
  184. validIn xs ys | valid xs && valid ys = length (head xs) == length ys
  185.              | otherwise = error "Matrices are not valid!"
  186.  
  187. multiplyM :: Matrix -> Matrix -> Matrix
  188. multiplyM [] _ = []
  189. multiplyM (x:xs) ys = foldr (\y acc -> dotProd x y : acc) [] ys : multiplyM xs ys
  190.  
  191. dotProd :: [Int] -> [Int] -> Int
  192. dotProd = (sum .) . zipWith (*)
  193.  
  194. transpose' :: [[a]] -> [[a]]
  195. transpose' [] = []
  196. transpose' xs = (foldr (\x acc -> (head x) : acc) [] xs) : transpose' [tail z | z <- xs, length z > 1]
  197.  
  198.  
  199.  
  200.  
  201.  
  202. -- Optional material
  203. -- 9.
  204.  
  205. isNN :: Matrix -> Bool
  206. isNN xs = length (head xs) == length xs
  207.  
  208. invertM :: Matrix -> [[Rational]]
  209. invertM x | isNN x = rationalM x
  210.          | otherwise = error "Only n*n matrices have inverses!!!"
  211.  
  212. rationalM :: Matrix -> [[Rational]]
  213. rationalM = map (map realToFrac)
  214.  
  215. det :: [[Rational]] -> [[Rational]]
  216. det xss | length xss == 2 = undefined
  217.        | otherwise = undefined
  218.  
  219. removeIndexRow :: Int -> [Rational] -> [Rational]
  220. removeIndexRow i xs = take i xs ++ drop (i + 1) xs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement