1. point-free Rewrite the following two definitions into a point-free form, i.e., f = ..., g = ..., using neither lambda-expressions nor list comprehensions nor enumeration nor where-clause nor let-clause: f x y = x / (8 - y) g x y = [x z | z <- [5,8..y]] ANS 1. (0.4p) f = flip $ (flip (/)).(8-) OR: flip (.) ((-) 8) . (/) OR: (. (8-) ) . (/) (0.6p) g = flip $ (flip map) . (flip takeWhile $ iterate (+3) 5) . (>=) --------------- 2. Explain the meaning and differences between the three type declarations: type, newtype, and data. ANS 2. (0.3) type - synonym (0.3) data - abstract ADT (0.4) newtype - as data, but only single unary constructor ------------------------------------------------------------ 3. Consider the following function: eliminatem n [] g = Just g eliminatem n (s:ss) g = eliminate n s g >>= eliminatem n ss 3.1. (0.4p) Given that the type of g is Grid, g :: Grid write the signature for eliminatem. Assume the most generic type for eliminate. ANS 3.1: eliminatem :: a -> [b] -> Grid -> Maybe Grid 3.2. (0.2p) How would your answer look like if the first line were changed to eliminatem n [] g = [g] ANS 3.2: eliminatem :: a -> [b] -> Grid -> [Grid] 3.3. (0.4p) Would the second line of the definition be correct after this change? Answer YES or NO, and motivate. YES, just another monad --------------- 4. Redefine map f and filter f using foldr. ANS 4. map f = foldr (\x xs -> f x : xs) [] OR map f = foldr ((:).f) [] filter f = foldr (\x xs -> if f x then x : xs else xs) [] --------------- 5. What is the effect and type of: (0.2p each) 5.1. uncurry ($) :: (a->b,a) -> b takes a pair: (funtion, argument) and returns the result of applying this function to the argument 5.2. uncurry (:) :: (a,[a]) -> [a] takes a pair (elem::a, list::[a]) and returns the list with elem inserted at the front 5.3. uncurry (.) :: (b->c,a->b) -> a -> c takes a pair of functions (suitably typed) and returns their composition 5.4. uncurry uncurry :: (a->b->c, (a,b)) -> c takes the pair (2-arg function in curried form, pair of arguments) and returns the result of applying the function consecutively to the first and second elem of the pair 5.5. curry uncurry TYPE ERROR ------------------------------------------------------------ 6. Given the following three lists of tuples: (personnummer, (efternamn, förnamn)): pn :: [ (String, (String,String)) ] (personnummer, sTiL-ID): ps :: [ (String, String) ] (sTiL-ID, epost): se :: [ (String, String) ] write a function that generates a list of pairs of the format (name, epost) ne :: [ (String, String) ] where name is a string of the form "förnamn efternamn". You may assume that the ps list contains all personal numbers from pn, while se contains all the StiL-IDs from ps. Document your function with a nice signature. ANS 6: getname :: (String, String) -> String getname (e,f) = f ++ " " ++ e ne = map lukup ps where lukup (p,n) = (getname n, fromJust $ lookup (fromJust $ lookup p ps) se)