Advertisement
Guest User

test

a guest
Oct 19th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. -- Informatics 1 - Functional Programming
  2. -- Tutorial 3
  3. --
  4. -- Week 5 - Due: 19-20 Oct.
  5.  
  6. module Tutorial3 where
  7.  
  8. import Data.Char
  9. import Test.QuickCheck
  10.  
  11.  
  12.  
  13. -- 1. Map
  14. -- a.
  15. uppers :: String -> String
  16. uppers xs = map toUpper xs
  17.  
  18. -- b.
  19. doubles :: [Int] -> [Int]
  20. doubles xs = map (*2) xs
  21.  
  22. -- c.
  23. penceToPounds :: [Int] -> [Float]
  24. penceToPounds xs = map (\x -> (fromIntegral x)/100) xs
  25.  
  26. -- d.
  27. uppers' :: String -> String
  28. uppers' xs = [toUpper x | x <- xs]
  29.  
  30. prop_uppers :: String -> Bool
  31. prop_uppers = undefined
  32.  
  33.  
  34.  
  35. -- 2. Filter
  36. -- a.
  37. alphas :: String -> String
  38. alphas x = filter (\xs -> (elem xs ['a'..'z']) || (elem xs ['A'..'Z'])) x
  39.  
  40. -- b.
  41. rmChar :: Char -> String -> String
  42. rmChar x xs = filter (\xb -> (xb /= x)) xs
  43.  
  44. -- c.
  45. above :: Int -> [Int] -> [Int]
  46. above x xs = filter (\xb -> (xb > x)) xs
  47.  
  48. -- d.
  49. unequals :: [(Int,Int)] -> [(Int,Int)]
  50. unequals x = filter (\(a,b) -> (a /= b)) x
  51.  
  52. -- e.
  53. rmCharComp :: Char -> String -> String
  54. rmCharComp xc xs = [x |x <- xs, x /= xc ]
  55.  
  56. prop_rmChar :: Char -> String -> Bool
  57. prop_rmChar = undefined
  58.  
  59.  
  60.  
  61. -- 3. Comprehensions vs. map & filter
  62. -- a.
  63. upperChars :: String -> String
  64. upperChars s = [toUpper c | c <- s, isAlpha c]
  65.  
  66. upperChars' :: String -> String
  67. upperChars' x = filter (\xb -> (elem xb ['A'..'Z'])) x
  68.  
  69. prop_upperChars :: String -> Bool
  70. prop_upperChars s = upperChars s == upperChars' s
  71.  
  72. -- b.
  73. largeDoubles :: [Int] -> [Int]
  74. largeDoubles xs = [2 * x | x <- xs, x > 3]
  75.  
  76. largeDoubles' :: [Int] -> [Int]
  77. largeDoubles' x = filter (>3) (map (*2) x)
  78.  
  79. prop_largeDoubles :: [Int] -> Bool
  80. prop_largeDoubles xs = largeDoubles xs == largeDoubles' xs
  81.  
  82. -- c.
  83. reverseEven :: [String] -> [String]
  84. reverseEven strs = [reverse s | s <- strs, even (length s)]
  85.  
  86. reverseEven' :: [String] -> [String]
  87. reverseEven' sts = filter (\st -> (even (length st)) ) (map (\sta -> (reverse sta)) sts)
  88.  
  89. prop_reverseEven :: [String] -> Bool
  90. prop_reverseEven strs = reverseEven strs == reverseEven' strs
  91.  
  92.  
  93.  
  94. -- 4. Foldr
  95. -- a.
  96. productRec :: [Int] -> Int
  97. productRec [] = 1
  98. productRec (x:xs) = x * productRec xs
  99.  
  100. productFold :: [Int] -> Int
  101. productFold xs = foldr (*) 0 xs
  102.  
  103. prop_product :: [Int] -> Bool
  104. prop_product xs = productRec xs == productFold xs
  105.  
  106. -- b.
  107. andRec :: [Bool] -> Bool
  108. andRec [] = True
  109. andRec (x:xs)
  110. |x == False = False
  111. |otherwise = andRec xs
  112.  
  113. andFold :: [Bool] -> Bool
  114. andFold xs = foldl(\acc x -> if x == False then False else acc) True xs
  115.  
  116. prop_and :: [Bool] -> Bool
  117. prop_and xs = andRec xs == andFold xs
  118.  
  119. -- c.
  120. concatRec :: [[a]] -> [a]
  121. concatRec [[]] = []
  122. concatRec (x:xs) = x ++ concatRec( xs )
  123.  
  124. concatFold :: [[a]] -> [a]
  125. concatFold = undefined
  126.  
  127. prop_concat :: [String] -> Bool
  128. prop_concat strs = concatRec strs == concatFold strs
  129.  
  130. -- d.
  131. rmCharsRec :: String -> String -> String
  132. rmCharsRec [] ys = ys
  133. rmCharsRec (x:xs) ys = rmCharsRec xs (rmChar x ys);
  134.  
  135.  
  136. rmCharsFold :: String -> String -> String
  137. rmCharsFold = undefined
  138.  
  139. prop_rmChars :: String -> String -> Bool
  140. prop_rmChars chars str = rmCharsRec chars str == rmCharsFold chars str
  141.  
  142.  
  143.  
  144. type Matrix = [[Int]]
  145.  
  146.  
  147. -- 5
  148. -- a.
  149. uniform :: [Int] -> Bool
  150. uniform xs = all (head(head xs)) []
  151.  
  152. -- b.
  153. valid :: Matrix -> Bool
  154. valid = undefined
  155.  
  156. -- 6.
  157.  
  158. -- 7.
  159. plusM :: Matrix -> Matrix -> Matrix
  160. plusM = undefined
  161.  
  162. -- 8.
  163. timesM :: Matrix -> Matrix -> Matrix
  164. timesM = undefined
  165.  
  166. -- Optional material
  167. -- 9.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement