0/23 Questions Answered
Mid-Semester Exam II
STUDENT NAME
Search students by name or email… ” Q1 Instructions
0 Points
You must acknowledge the following integrity pledge before proceeding. Please read carefully and check all the boxes.
I am committed to being a person of integrity.
I pledge, as a member of the Australian National University community, to abide by and uphold the standards of academic integrity outlined in the ANU statement on honesty and plagiarism, and I am aware of the relevant legislation, and understand the consequences of me breaching those rules.
I will not actively communicate in any way with anyone else during this exam. This includes asking questions in any online forum.
Read and check off the following instructions:
1. You will need to be able to upload code files to this browser from your Haskell workspace. You must also make sure all code you upload compiles without errors.
Make sure this browser window is open on the machine where you plan to work.
If you are working on the ANU Linux VDI this browser window should be open in Linux, not on your personal machine.
If this browser window is not open where you plan to work close this window now and log back into the exam in a browser on your work machine.
2. This examination is timed.
Note the remaining time at the top right of this screen. Set an alarm for yourself if you need one.
3. Permitted materials. This is an open book exam. You might in particular find the course website, the Prelude documentation, and the Data.List documentation useful.
You may use any documentation you wish but all work must be your own.
Save Answer
Q2 Commands 1 Point
What does the Linux shell command ls do? # Lists files in a folder.
# Creates a new folder.
# Changes the current folder.
# Copies a folder. Save Answer
Q3 Programming paradigms 5 Points
For each of the following statements select True if it is correct or False otherwise.
Each correct answer gains you 1 point while each incorrect answer loses you 0.5 point. An unanswered question neither gains nor loses you points.
1. Haskell is a binary language. True
False
2. Haskell is an assembly language. True
False
3. Haskell is a high-level language. True
False
4. Haskell is a functional programming language. True
False
5. Haskell is a declarative programming language. True
False
Save Answer
Q4 Number Systems 4 Points
Consider the expression
11112 + F16 + 1510
where the subscript denotes the base of a numbering system.
For each of the following select True if the equality holds or False otherwise.
Each correct answer gains you 1 point while each incorrect answer loses you 0.5 point. An unanswered question neither gains nor loses you points.
1. 11112 + F16 + 1510 = 4510 True
False
2. 11112 + F16 + 1510 = 3010 True
False
3. 11112 + F16 + 1510 = FFF16 True
False
4. 11112 + F16 + 1510 = 1011012 True
False
Save Answer
Q5 Problem Classes 1 Point
Consider the problem of choosing the next move in a game where players have perfect knowledge of the state of the game and the moves available. Which class of problem does this fall into:
# Function problem
# Decision problem
# Search problem
# Optimisation problem
Save Answer
Q6 Sets 2 Points
Consider the sets Error = {Y, N}, and Op = {+, -, *}
Which one of the following is the set Error + Op?
# {(Y, left), (N, left), (+, right), (-, right), (*, right)} # {(Y, +), (Y, -), (Y, *), (N, +), (N, -), (N, *)}
# {{Y, N}, {+, -, *}}
# {(Y, 1), (N, 2), (+, 3), (-, 4), (*, 5)}
# {(Y, left), (N, right), (+, left), (-, centre), (*, right)}
Save Answer
Q7 Function Composition 5 Points
Recall that R is the set of real numbers, and R+ is the set of non- negative real numbers (including 0). Note that every element of R+ is also an element of R: that is, R+ ⊂ R.
Consider functions log :: R+ → R and exp :: R → R+.
For each of the following function compositions select True if it is
valid or False otherwise.
Each correct answer gains you 1 point while each incorrect answer loses you 0.5 point. An unanswered question neither gains nor loses you points.
1. exp ∘ log is a valid function composition. True
False
2. log ∘ exp is a valid function composition. True
False
3. log ∘ log is a valid function composition. True
False
4. exp ∘ exp is a valid function composition. True
False
5. log∘exp∘exp True
False
Save Answer
Q8 Haskell Function Signatures 5 Points
For each of the following, select True if it is a legal function signature or False otherwise.
Each correct answer gains you 1 point while each incorrect answer loses you 0.5 point. An unanswered question neither gains nor loses you points.
1. foo :: Int -> Int True
False
2. foo :: (Int) -> Int True
False
3. foo :: (Int, Int) -> (Int, Int) True
False
4. foo :: [a] -> Int True
False
5. foo::()->() True
False
Save Answer
Q9 Function Definitions. 4 Points
For each of the following select True if it is a valid Haskell function definition or False otherwise.
Each correct answer gains you 1 point while each incorrect answer loses you 0.5 point. An unanswered question neither gains nor loses you points.
1. (||)ab=a||b True
False
2. a||b|a=True | b = True
True False
3. False || False = False _ || _ = True
True False
4. False||b=b True ||_=True
True False
Save Answer
Q10 Algebraic Data Types 4 Points
Consider the following Haskel algebraic data type:
data Shape = Circle Float | Rectangle Float Float For each of the following select True if it is a valid Haskell function
definition or False otherwise.
Each correct answer gains you 1 point while each incorrect answer loses you 0.5 point. An unanswered question neither gains nor loses you points.
1. convert :: Shape -> Shape
convert (Rectangle x y) = Circle (x * y)
True False
2. area :: Shape -> Float area (Circle r) = pi * r^2
True False
3. circumference :: Circle -> Float circumference (Circle r) = 2 * pi * r
True False
4. perimeter :: Rectangle -> Float perimeter (Rectangle w h) = 2 * (w + h)
True False
Save Answer
Q11 Function Application 1 Point
Consider the following Haskell function definition:
add :: Int -> Int -> Int
add x y = x + y
Which of the following is an incorrect application of this function?
# add (2, 3) # add 2 3
# add 2
# (add 2) 3 # (add 2 3)
Save Answer
Q12 Basic Types 5 Points
For each of the following select True if the statement is correct or False otherwise.
Each correct answer gains you 1 point while each incorrect answer loses you 0.5 point. An unanswered question neither gains nor loses you points.
1. ([], “abc”, [True]) is a value of type ([Int], [Char], Bool) .
True False
2. (1,”12″, 2) is a value of type (Integer, [Char], Int) . True
False
3. Type Bool is an instance of the type class Show . True
False
4. A tuple can have an infinite number of elements. True
False
5. New types can be declared using the keywords newtype and data .
True False
Save Answer
Q13 Control Structures 4 Points
Consider the following function definition:
removeAt :: Int -> [a] -> [a]
removeAt n (x:xs) = x : removeAt (n – 1) xs
For each of the following select True if it is an equivalent function definition or False otherwise.
Each correct answer gains you 1 point while each incorrect answer loses you 0.5 point. An unanswered question neither gains nor loses you points.
1. removeAt :: Int -> [a] -> [a]
(y:ys) -> y : removeAt (n-1) ys True
False
2. removeAt :: Int -> [a] -> [a]
_ -> y : removeAt (n-1) ys True
False
3. removeAt :: Int -> [a] -> [a]
GT -> x : removeAt (n-1) xs True
False
4. removeAt :: Int -> [a] -> [a]
removeAt n xs = take n xs ++ drop (n+1) xs
True False
Save Answer
Q14 Lists 5 Points
For each of the following statements select True if it is correct or False otherwise.
Each correct answer gains you 1 point while each incorrect answer loses you 0.5 point. An unanswered question neither gains nor loses you points.
1. Lists can be constructed recursively using : . True
False
2. Haskell is capable of representing integers with arbitrary precision.
True False
3. The length of the list [(x,y) | x <- a, y <- b] is the length of a plus the length of b .
True False
4. The following are all valid expressions of the same value:
[1,2,3]
1:2:3:[]
1:2:[3]
1:[2,3]
[] ++ 1 : [2,3] True
False
5. The following expressions are equivalent:
[(x,y) | x <- [1,2,3], y <- [4,5]]
[(x,y) | y <- [4,5], x <- [1,2,3]]
True False
Save Answer
Q15 Lists 5 Points
Consider the following Haskell function:
replicate n x = [x | n <- [1..n]]
For each of the following statements select True if it is correct or
False otherwise.
Each correct answer gains you 1 point while each incorrect answer loses you 0.5 point. An unanswered question neither gains nor loses you points.
1. Function replicate is polymorphic. True
False
2. A possible type signature for replicate is
Integer -> String -> [[Char]] . True
False
3. The following is an equivalent implementation of function
replicate :
replicate n x
| n > 0 = x : replicate (n – 1) x
| otherwise = []
True False
4. The following is an equivalent implementation of function replicate :
replicate n x = take n [x,x..] True
False
5. Function reverse acts as an identity on function replicate . That is: reverse . replicate == replicate .
True False
Save Answer
Q16 Expressions 4 Points
For each of the following Haskell expressions select True if it evaluates without error or False otherwise.
Each correct answer gains you 1 point while each incorrect answer loses you 0.5 point. An unanswered question neither gains nor loses you points.
removeAt 0 (x:xs) = xs
removeAt _ [] = []
removeAt n xs
| n == 0 = case xs of
[] -> []
(y:ys) -> ys
| otherwise = case xs of
[] -> []
removeAt n xs = case xs of
(y:ys) -> case n of
0 -> ys
removeAt _ [] = []
removeAt n (x:xs) = case compare n 0 of
LT -> []
EQ -> xs
1:2:[] ++ [3]
[1] ++ [2,3]
(5 :: Double) + read (show (pi :: Float)) True
False
1/5 == recip 5 True
False
read (show (read “123” :: Double)) :: Int True
False
read (show (read “123” :: Double)) :: Double True
False
Save Answer
Q17 Polymorphic Functions 4 Points
For each of the following functions select True if it has polymorphic type or False otherwise.
Each correct answer gains you 1 point while each incorrect answer loses you 0.5 point. An unanswered question neither gains nor loses you points.
1. average [] = 0
average xs = div (sum xs) (length xs)
True False
2. halve xs = (take (div (length xs) 2) xs, drop (div (length xs) True
False
3. adjPairs xs = zip xs (tail xs) True
False
4. firstsps=[x|(x,_)<-ps] True
False
Save Answer
Q18 Maybe Type 4 Points
For each of the following function definitions select True if it is valid or False otherwise.
Each correct answer gains you 1 point while each incorrect answer loses you 0.5 point. An unanswered question neither gains nor loses you points.
1. fib :: Int -> Maybe Int
n -> fib’ (n-2) + fib’ (n-1)
True False
2. safeDiv :: Int -> Int -> Maybe Int safeDiv _ 0 = Just 0
safeDiv m n = Just (m `div` n) True
False
3. unMaybe :: [Maybe a] -> [a]
Nothing:xs -> unMaybe xs
True False
4. safefact :: Int -> Maybe Int
| otherwise = Just (n * safefact (n – 1)) True
False
Save Answer
Q19 Operator Types 5 Points
For each of the following operators from the Haskell prelude select True if it has the type signature Num a => a -> a -> a or False otherwise.
Each correct answer gains you 1 point while each incorrect answer loses you 0.5 point. An unanswered question neither gains nor loses you points.
1. (+) True
False
2. (++) True
False
3. (/) True
False
4. (<) True
False
5. (*) True
False
Save Answer
Q20 Haskell Programming 32 Points
The following programming questions use template files which you can access by clicking on the links in the questions below.
You must submit your answer using the link provided in each question to upload the file. You will not be able to push your solution to gitlab.
As an alternative to clicking on the links, you can clone the files from the gitlab repository at https://gitlab.cecs.anu.edu.au/comp1100/comp1100-2020s2-mse2 using the URL https://gitlab.cecs.anu.edu.au/comp1100/comp1100- 2020s2-mse2.git with the command:
git clone https://gitlab.cecs.anu.edu.au/comp1100/comp1100-2020s2
Q20.1 Control expressions 6 Points
Click on the link PotatoPrice.hs to download a Haskell script containing a template for the function potatoPrice .
If your browser downloads the file as PotatoPrice.hs.txt with the extension .txt make sure to rename the file to
PotatoPrice.hs .
You must complete the function potatoPrice .
Make sure to edit and test using ghci before uploading using the following link:
$ Please select file(s) Select file(s) Save Answer
Q20.2 Recursion 8 Points
Click on the link Replace.hs to download a Haskell script containing a template (instructions included) for the function replace . Given two characters (call them old and new ) and a string as input, the function replaces any appearance of the old character in the string with the new character.
If your browser downloads the file as Replace.hs.txt with the extension .txt make sure to rename the file to Replace.hs .
You must use recursion in completing the function replace . Make sure to edit and test using ghci before uploading using the
following link:
$ Please select file(s) Select file(s)
Save Answer
Q20.3 Recursion on Lists 8 Points
Click on the link FindDouble.hs to download a Haskell script containing a template (instructions included) for the function
findDouble that returns True if and only if there is a pair of adjacent equal numbers in the list, and False otherwise.
If your browser downloads the file as FindDouble.hs.txt with the extension .txt make sure to rename the file to
FindDouble.hs .
You must use recursion in completing the function FindDouble .
Make sure to edit and test using ghci before uploading using the following link:
$ Please select file(s) Select file(s) Save Answer
Q20.4 Recursion on Lists 10 Points
Click on the link ListReplicate.hs to download a Haskell script containing a template (instructions included) for the function
listReplicate that takes a list of elements and replicates each of the elements n times, returning a concatenated list.
HINT: Use the Haskell prelude function replicate n x which returns a list of n copies of x
If your browser downloads the file as ListReplicate.hs.txt with the extension .txt make sure to rename the file to
ListReplicate.hs .
You must use recursion in completing the function
ListReplicate .
Make sure to edit and test using ghci before uploading using the
following link:
$ Please select file(s) Select file(s)
Save Answer
1.
2.
3.
4.
fibn =ifn>0thenJust(fib’n)elseNothing
where fib’ n = case n of
0 -> 0
1 -> 1
unMaybe xs = case xs of
[] -> []
(Just x):xs -> x : (unMaybe xs)
safefact n
| n < 0 = Nothing
|n==1 =Just1
Save All Answers
Submit & View Submission !
2
-ms