Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Informatics 1 - Functional Programming
- -- Tutorial 3
- --
- -- Week 5 - Due: 19-20 Oct.
- module Tutorial3 where
- import Data.Char
- import Test.QuickCheck
- -- 1. Map
- -- a.
- uppers :: String -> String
- uppers xs = map toUpper xs
- -- b.
- doubles :: [Int] -> [Int]
- doubles xs = map (*2) xs
- -- c.
- penceToPounds :: [Int] -> [Float]
- penceToPounds xs = map (\x -> (fromIntegral x)/100) xs
- -- d.
- uppers' :: String -> String
- uppers' xs = [toUpper x | x <- xs]
- prop_uppers :: String -> Bool
- prop_uppers = undefined
- -- 2. Filter
- -- a.
- alphas :: String -> String
- alphas x = filter (\xs -> (elem xs ['a'..'z']) || (elem xs ['A'..'Z'])) x
- -- b.
- rmChar :: Char -> String -> String
- rmChar x xs = filter (\xb -> (xb /= x)) xs
- -- c.
- above :: Int -> [Int] -> [Int]
- above x xs = filter (\xb -> (xb > x)) xs
- -- d.
- unequals :: [(Int,Int)] -> [(Int,Int)]
- unequals x = filter (\(a,b) -> (a /= b)) x
- -- e.
- rmCharComp :: Char -> String -> String
- rmCharComp xc xs = [x |x <- xs, x /= xc ]
- prop_rmChar :: Char -> String -> Bool
- prop_rmChar = undefined
- -- 3. Comprehensions vs. map & filter
- -- a.
- upperChars :: String -> String
- upperChars s = [toUpper c | c <- s, isAlpha c]
- upperChars' :: String -> String
- upperChars' x = filter (\xb -> (elem xb ['A'..'Z'])) x
- prop_upperChars :: String -> Bool
- prop_upperChars s = upperChars s == upperChars' s
- -- b.
- largeDoubles :: [Int] -> [Int]
- largeDoubles xs = [2 * x | x <- xs, x > 3]
- largeDoubles' :: [Int] -> [Int]
- largeDoubles' x = filter (>3) (map (*2) x)
- prop_largeDoubles :: [Int] -> Bool
- prop_largeDoubles xs = largeDoubles xs == largeDoubles' xs
- -- c.
- reverseEven :: [String] -> [String]
- reverseEven strs = [reverse s | s <- strs, even (length s)]
- reverseEven' :: [String] -> [String]
- reverseEven' sts = filter (\st -> (even (length st)) ) (map (\sta -> (reverse sta)) sts)
- prop_reverseEven :: [String] -> Bool
- prop_reverseEven strs = reverseEven strs == reverseEven' strs
- -- 4. Foldr
- -- a.
- productRec :: [Int] -> Int
- productRec [] = 1
- productRec (x:xs) = x * productRec xs
- productFold :: [Int] -> Int
- productFold xs = foldr (*) 0 xs
- prop_product :: [Int] -> Bool
- prop_product xs = productRec xs == productFold xs
- -- b.
- andRec :: [Bool] -> Bool
- andRec [] = True
- andRec (x:xs)
- |x == False = False
- |otherwise = andRec xs
- andFold :: [Bool] -> Bool
- andFold xs = foldl(\acc x -> if x == False then False else acc) True xs
- prop_and :: [Bool] -> Bool
- prop_and xs = andRec xs == andFold xs
- -- c.
- concatRec :: [[a]] -> [a]
- concatRec [[]] = []
- concatRec (x:xs) = x ++ concatRec( xs )
- concatFold :: [[a]] -> [a]
- concatFold = undefined
- prop_concat :: [String] -> Bool
- prop_concat strs = concatRec strs == concatFold strs
- -- d.
- rmCharsRec :: String -> String -> String
- rmCharsRec [] ys = ys
- rmCharsRec (x:xs) ys = rmCharsRec xs (rmChar x ys);
- rmCharsFold :: String -> String -> String
- rmCharsFold = undefined
- prop_rmChars :: String -> String -> Bool
- prop_rmChars chars str = rmCharsRec chars str == rmCharsFold chars str
- type Matrix = [[Int]]
- -- 5
- -- a.
- uniform :: [Int] -> Bool
- uniform xs = all (head(head xs)) []
- -- b.
- valid :: Matrix -> Bool
- valid = undefined
- -- 6.
- -- 7.
- plusM :: Matrix -> Matrix -> Matrix
- plusM = undefined
- -- 8.
- timesM :: Matrix -> Matrix -> Matrix
- timesM = undefined
- -- Optional material
- -- 9.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement