## 辅导案例-COM2108

• August 13, 2020

COM2108 COM2108 1 TURN OVER Data Provided: None DEPARTMENT OF COMPUTER SCIENCE August 2020 Functional Programming Answer ALL questions. Question 1 carries 40 marks; questions 2 and 3 carry 30 marks each. Figures in square brackets indicate the number of available marks allocated to each part of a question. COM2108 COM2108 2 CONTINUED THIS PAGE IS BLANK COM2108 COM2108 3 TURN OVER 1. Say a function was defined as follows: fn1 :: Int -> Int fn1 0 = 1 fn1 n = n * (fn1 (n-1)) If the function was applied to the input 5, it would be expanded and evaluated as: fn1 5 = 5 * (fn1 (5-1)) = 5 * (4 * (fn1 (4-1))) = 5 * (4 * (3 * (fn1(3-1)))) = 5 * (4 * (3 * (2 * (fn1(2-1))))) = 5 * (4 * (3 * (2 * (1 * (fn1(1-0)))))) = 5 * (4 * (3 * (2 * (1 * (1))))) = 5 * (4 * (3 * (2 * 1))) = 5 * (4 * (3 * 2)) = 5 * (4 * 6) = 5 * 24 = 120 a) Consider this recursive function: fn2 [] = [] fn2 (c:cs) = fn2 cs ++ [c] Using the same approach as above, show the expansion and result for the application of this function to “abcd”. You must show the full expansion; the result alone will not earn you any marks. [10 marks] b) Consider this recursive function: fn3 :: Int -> Int fn3 n = fnAcc n 1 where fnAcc n acc = fnAcc (n-1) (n*acc) The intention of this function is to achieve the same result as fn1 above. What is missing from this function? Rewrite the full function with the missing segment inserted. [10 marks] QUESTION CONTINUES ON THE NEXT PAGE COM2108 COM2108 4 CONTINUED c) Show the expansion and evaluation of fn3 5 assuming that the correction in question 1 b) has been made. [10 marks] d) Which of fn1 and (the corrected version of) fn3 is more efficient? Why? [10 marks] COM2108 COM2108 5 TURN OVER 2. Consider the following function: fn4 = length . filter (not . isSpace) isSpace is found in the module Data.Char It takes a character and returns True if it is whitespace, False otherwise. a) What is the type of fn4? [5 marks] b) Re-write fn4 so that it doesn’t use the dot (.) operator but produces the same result. [10 marks] c) What does fn4 do? Write a brief description of the form “fn4 takes … as an argument and returns …” which explains the relationship between the input and the output. (For example “fn4 takes a floating point number and returns True if it is within 0.5 of a prime number, and False otherwise”. Note that this is not what fn4 does! [10 marks] d) The type of the function composition operator is given as (.) :: (b -> c) -> (a -> b) -> (a -> c) Given functions f, g, h with the following type declarations: f :: Int -> Int g :: Int -> Bool h :: Bool -> Bool Which of these function definitions are valid, and where valid, what is the type of the function? (i) x = f.g (ii) y = g.h (iii) z = g.f [15 marks] COM2108 COM2108 6 3. Consider the following Haskell code: type Candidate = [Char] type MixedResult = (Candidate, Float, Float) type FinalResult = (Candidate, Float) validMark :: Float -> Bool validMark n = n >= 0 && n <= 100 validMixedResult :: MixedResult -> Bool validMixedResult (cand, m1, m2) = validMark m1 && validMark m2 getConcerns :: [MixedResult] -> [MixedResult] getConcerns list = [ x | x a) The code is intended to highlight problems with assessment results where the work has been assessed by two different markers (the MixedResult). Given the current definitions, if getConcerns is run over a list of MixedResult, what would be flagged as a concern? [5 marks] b) There are additional rules that should be applied to highlight concerns. As well as the existing rules, the following should be checked: (i) The two marks should be within a grade band ( < 40, 40 - < 50, 50 - < 60, 60 - < 70, 70+) (ii) The two marks should not differ by > 10 Re-write the function validMixedResult so that it also ensures that these rules are satisfied. (You may write additional functions if you wish.) Your code should be well-structured as well as correct. [10 marks] c) For mixed results that are valid, the final result should be calculated as the average of the two marks. Write a function that takes a list of MixedResult as input and returns a list of FinalResult for the items in the input list that do not violate the rules. As before, your code should be well-structured as well as correct. [15 marks] END OF QUESTION PAPER

• #### COST CALCULATOR

计算费用

###### MOST POPULAR

ezAce多年来为广大留学生提供定制写作、留学文书定制、语法润色以及网课代修等服务，超过200位指导老师为您提供24小时不间断地服务。