All of your answers will be auto-graded, so for coding problems you will be marked according to how many of our tests you pass. Incorrect answers that pass tests will be penalised accordingly. The auto-grader will only mark code that you upload to the exam.
Reading period: 10 minutes
Writing period: 60 minutes
Permitted materials: None, aside from the software environment provided.
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
[15 Marks]
Multiple choice questions
For each of the following questions, mark the correct answer.
1 i)
[2.5 Marks]
Sets and functions
Let ℤ be the set of integers and 𝔹 be the set of Booleans {False,True}.
Which of the following is an element of ℤ + 𝔹?
3 | |
(3,left) | |
(3,right) | |
(3,False) | |
(False,3) | |
1 ii)
[2.5 Marks]
Sets and functions
Which of the following is an element of ℤ × 𝔹?
3 | |
(3,left) | |
(3,right) | |
(3,False) | |
(False,3) | |
1 iii)
[2.5 Marks]
Sets and functions
Let the mathematical function f :: ℤ ⟶ ℤ
be defined as f(x) = 3 - x
.
Let the mathematical function g :: ℤ ⟶ ℤ
be defined as g(x) = 2 * x
.
Recall that .
is the symbol for composition. What is the result of applying the function g . f
to the integer 5?
−7 | |
−4 | |
4 | |
7 | |
No answer: g . f is not well defined | |
1 iv)
[2.5 Marks]
Type signatures
Which of the following is not a valid Haskell type signature?
foo :: Int -> Int | |
foo :: Int -> Integer | |
foo :: Int -> (Int | Int) | |
foo :: Int -> (Int,Int) | |
foo :: Int -> Maybe Int | |
1 v)
[2.5 Marks]
Type signatures
Which of the following is a valid type signature for the following Haskell function?split b = case b of
False -> 0
True -> "one"
split :: Bool -> Int | |
split :: Bool -> String | |
split :: Bool -> (Left Int | Right String) | |
This function cannot be given a type because its return value cannot be given a type | |
This function cannot be given a type because pattern matching on Bool should be done with guards, not case | |
1 vi)
[2.5 Marks]
Type signatures
Which of the following is a valid Haskell type signature for the following Haskell function?bar x y = x : x : y
bar :: Char -> Char -> String -> String | |
bar :: (Char -> Char) -> String -> String | |
bar :: Char -> String -> String | |
bar :: (Char -> String) -> String | |
bar :: (Char,String) -> String | |
Question 2
[5 Marks]
True/False questions
Consider the following type signaturebaz :: Bool -> Bool
Mark True for each of the following statements if it is correct, and False if not.
True | False | ||
baz must be defined using a case expression or guarded expression | |||
baz True || baz False is a valid Haskell expression | |||
If you run baz True you will certainly get either True or False as your result | |||
There is a Prelude function with the same type signature as baz | |||
The output of baz depends only on its Bool input |
Question 3
[5 Marks]
Days of the Week (DaysOfTheWeek.hs)
Using the incomplete template DaysOfTheWeek.hs
in your midterm exam IntelliJ project, complete the undefined function isWeekend
. The specification of the isWeekend
function is provided in the comments immediately above the function. You will need to adhere to that specification. You may use the doctests provided to help test your solution.
You can upload your implementation of DaysOfTheWeek.hs by dragging your file into the box below. If you drag your file into the box again, it will overwrite your previous upload. This question is auto-graded: you will be graded according to how many tests you pass. The tests used to grade your work may differ from those that appear in the template file. The auto-grader will only mark what you upload to the exam. Therefore it is essential that you upload your code into this exam, in the browser.
Question 4
[11 Marks]
Participants (Participants.hs)
Using the incomplete template Participants.hs
in your midterm exam IntelliJ project, complete the two undefined functions nameOf
and teamOf
.
The specifications of the functions are provided in the comments immediately above them. Instructions for submission are as for the previous question.
Question 5
[10 Marks]
Positive Ints (PositiveInts.hs)
Using the incomplete template PositiveInts.hs
in your midterm exam IntelliJ project, complete the two undefined functions startPositive
and countPositive
.
The specifications of the functions are provided in the comments immediately above them. Instructions for submission are as for the previous programming questions.
Question 6
[4 Marks]
Approximating Two (ApproximatingTwo.hs)
Using the incomplete template ApproximatingTwo.hs
in your midterm exam IntelliJ project, complete the undefined function twoApprox
.
The specification of the function is provided in the comments immediately above it. Instructions for submission are as for the previous programming questions.
Question 7
[10 Marks]
COMP1130 extension questions
For each of the following questions, mark the correct answer.
7 i)
[2.5 Marks]
Lambda calculus
Using scope and associativity rules, remove as many brackets as you can from the following term:
(λu.v)((w x)(λy.z))
(λu.v) w x λy.z | |
λu.v (w x λy.z) | |
(λu.v)(w x λy.z) | |
λu.v (w x (λy.z)) | |
(λu.v)(w x (λy.z)) | |
7 ii)
[2.5 Marks]
Lambda calculus
What is the set of free variables of the following term?
λy.(λx.x y)(λy.w x y)
{w} | |
{w,x} | |
{w,y} | |
{w,x,y} | |
{x,y} | |
7 iii)
[2.5 Marks]
Lambda calculus
Which of the answers below is alpha-equivalent to the following term?(λx.λx.λy.y x) y
(λw.λy.λz.z w) y | |
(λw.λy.λz.z w) z | |
(λw.λy.λz.z y) y | |
(λw.λy.λz.z y) z | |
(λw.λz.λz.z z) y | |
7 iv)
[2.5 Marks]
Lambda calculus
Beta-reduce the following term as far as is possible:(λx.x x)(λy.y z)x
x x z | |
x z (x z) | |
x z z | |
z x z | |
z z x | |