PROGRAMMING AS PROBLEM SOLVING COMP1100
checksum: AAAAA bytes: 109
Important:. Code templates for answers to coding questions in this exam can be found in the accompanying IntelliJ project. You can place this Web page for the exam in a window side-by-side with the IntelliJ project window, so that you can conveniently switch between them while working on your answers. You will also want to open the Terminal tool in IntelliJ in which you can load your solution codes into GHCi and test them out. You can also make use of QuickCheck and doctest in testing.
All your answers will be auto-graded, so for coding problems you will be marked according to how many of our tests you pass (including example tests provided to you in the comments). Incorrect answers that pass tests will be penalised accordingly. The auto-grader will only mark code that you upload to the exam. Therefore, it is essential that you upload your code into this exam in the boxes provided using the associate ‘Choose File’ buttons. This is most easily done by using the mouse to drag the file from IntelliJ onto the ‘Choose File’ button.
Reading period: 15 Minutes duration
Writing period: 180 Minutes duration.
Permitted materials: None, aside from the software environment provided.
You should attempt to answer all questions. Questions are not of equal value.
All questions must be completed on this web form.
Your work is automatically saved and recorded as you type. This is a closed examination. You may not copy this exam.
Question 1 [20 Marks] Functions
For each of the following questions, identify the choice that provides the best answer. Feel free to use software tools (IntelliJ, GHCi) to check your answers. If uncertain, use the ‘clear’ button to clear your answer.
Each correct answer gains you 2 marks, each incorrect answer loses you 1 mark, while a question left unanswered neither loses nor gains marks.
The final mark for this question is calculated by bounding the sum of marks between 0 and 20. For example, if you answered all questions correctly you would gain 20 for this question. If you answer 4 correctly and 4 incorrectly you would receive 4 marks for this question.
Consider the function:
double :: Integer -> Integer
double = (2 *)
1 i) [2 Marks] Application
What is the value of the following expression?
map double [1,4,4,3]
[1,1,4,4,4,4,3,3]
[2,8,8,6]
[1,4,4,3,1,4,4,3]
(2,8,8,6)
20
Clear
1 ii) [2 Marks] Composition
What is the value of the following expression?
map (double . double) [1,4,4,3]
[1,4,4,3,1,4,4,3]
(22,88,88,66)
[4,16,16,12]
40
[11,44,44,33]
Clear
1 iii) [2 Marks] Higher-order functions What is the value of the following expression?
map double []
[2]
0
()
[]
error
Clear
1 iv) [6 Marks] Equivalence
Now suppose sum :: [Integer] -> Integer is a function that sums a list of integers (analogously to the polymorphic
Haskell prelude function sum). Which of the following properties is true or false? True False
sum . map double == double . sum
sum . map sum == sum . concat
Clear Clear
sum . sort == sum
1 v) [6 Marks] Precedence
In Haskell, function application takes precedence over every other operator, so double 3+4 means (double 3)+4, not
double (3+4).
Which of the following expressions is a rendering of sin2 ¦È (that is, the square of sin ¦È)?
[In the following Haskell fragments exponentiation is denoted by (^), and sin :: Floating a => a -> a is the trigonometric function in the Haskell prelude implementing sin.]
True False
Clear Clear Clear
sin^2 theta
sin theta^2
(sin theta)^2
1 vi) [2 Marks] Precedence
How would you express (sin 2¦È) / 2¦Ð? sin (2 * theta) / (2 * pi)
sin (2 * theta) / 2 * pi
sin (2 * theta / 2 * pi)
Clear
Question 2 [24 Marks] Syntax, types, well-formed expressions
Some of the following expressions are not syntactically correct, while others are syntactically correct but do not have sensible types. Some are well-formed (syntactically correct and having a sensible type). Which of the following expressions is syntactically correct, and which is well-formed? In the case of a well-formed expression, identify a suitable type. Assume double :: Int -> Int. If you use the computer to check your answers be prepared for some strange error messages, and choose your corresponding answer carefully.
2 i) [2 Marks] [0,1)
not syntactically correct
syntactically correct, not sensibly typed well-formed [Int]
well-formed [Integer]
well-formed Num t => [t]
Clear
2 ii) [2 Marks] [0,1]
not syntactically correct
syntactically correct, not sensibly typed well-formed [Int]
well-formed [Integer]
Clear
well-formed Num t => [t] Clear
2 iii) [2 Marks] double -3
not syntactically correct
syntactically correct, not sensibly typed well-formed, Int
well-formed, Integer
well-formed, Num a => a
Clear
2 iv) [2 Marks] double (-3)
not syntactically correct
syntactically correct, not sensibly typed well-formed, Int
well-formed, Integer
well-formed, Num a => a
Clear
2 v) [2 Marks] double double 0
not syntactically correct
syntactically correct, not sensibly typed well-formed, Int
well-formed, Integer
well-formed, Num a => a
Clear
2 vi) [2 Marks] if 1==0 then 2==1
not syntactically correct
syntactically correct, not sensibly typed well-formed, Int
well-formed, Integer
well-formed, Bool
Clear
2 vii) [2 Marks]
if 1==0 then 2==1 else 3==2
not syntactically correct
syntactically correct, not sensibly typed well-formed, Int
well-formed, Integer
well-formed, Bool
Clear
2 viii) [2 Marks] “++” == “+” ++ “+”
not syntactically correct
syntactically correct, not sensibly typed well-formed, [Char]
well-formed, String
well-formed, Bool
Clear
2 ix) [2 Marks] [(+),(-)]
not syntactically correct
syntactically correct, not sensibly typed well-formed, [a]
well-formed, [(a),(b)]
well-formed, Num a => [a -> a -> a]
Clear
2 x) [2 Marks] [[],[[]],[[[]]]]
not syntactically correct
syntactically correct, not sensibly typed well-formed, [t]
well-formed, [[t]]
well-formed, [[[t]]]
well-formed, [[[[t]]]]
Clear
2 xi) [2 Marks]
concat [“tea”,”for”,’2′]
not syntactically correct
syntactically correct, not sensibly typed well-formed, Char
well-formed, [Char]
well-formed, [String]
Clear
2 xii) [2 Marks]
concat [“tea”,”for”,”2″]
not syntactically correct
syntactically correct, not sensibly typed well-formed, Char
well-formed, [Char]
well-formed, [String]
Clear
Question 3 [16 Marks] Lists
Which of the following properties are true for all xs and which are false? If uncertain, use the ‘clear’ button to clear your answer.
Each correct answer gains you 2 marks, each incorrect answer loses you 1 mark, while a question left unanswered neither loses nor gains marks.
The final mark for this question is calculated by bounding the sum of marks between 0 and 16. For example, if you answered all questions correctly you would gain 16 marks for this question. If you answer 8 correctly and 1 incorrectly you would receive 15 marks for this question.
[]:xs == xs
[]:xs == [[],xs]
xs:[] == xs
xs:[] == [xs]
xs:xs == [xs,xs]
[[]] ++ xs == xs
[[]] ++ xs == [[],xs]
[[]] ++ [xs] == [[],xs]
[xs] ++ [] == [xs]
True False
Clear Clear Clear Clear Clear Clear Clear Clear Clear
Question 4 [20 Marks] Strings and tuples(Date.hs)
Suppose a date is represented by three integers (day,month,year). Define a function showDate :: Date -> String so that, for example,
showDate (10,12,2013) = “10th December, 2013”
showDate (21,11,2020) = “21st November, 2020”
Starting from the incomplete template Date.hs in your exam IntelliJ project, complete the function definitions there to solve the problem, by replacing the undefined bodies with your solutions. The comments show examples of the behaviour for each function, which you can try in GHCi, or run as tests using the command doctest Date.
Upload your code here: Choose File no file selected
Question 5 [20 Marks] Strings and lists (Anagrams.hs)
Consider a book with the title EHT CDOORRSSW AAGMNR ACDIINORTY. It contains lists of entries like this:
Clear code
6-letter words
————–
…
eginor: ignore,region
eginrr: ringer
eginrs: resign,signer,singer
…
Perhaps by now you have guessed that the book title is an anagram for THE CROSSWORD ANAGRAM DICTIONARY. The book is an anagram dictionary. The letters of the anagrams are sorted and the results are stored in dictionary order. Associated with each anagram are the words with the same letters. Your task is to implement a function
anagrams :: Int -> [Word] -> String
so that anagrams n takes a list of words in alphabetical order, extracts just the n-letter words, and produces a string that, when displayed, gives a list of the anagram entries for the n-letter words. We have already decomposed the problem into smaller tasks for you, which you must implement to achieve the desired result.
Starting from the incomplete template Anagrams.hs in your exam IntelliJ project, complete the function definitions there to solve the problem, by replacing the undefined bodies with your solutions. The comments show examples of the behaviour for each function, which you can try in GHCi, or run as tests using the command doctest Anagrams.
Upload your code here: Choose File no file selected
Clear code