0/21 Questions Answered
Final Exam (Supplementary)
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 Polymorphic type signatures 5 Points
For each of the following select True if the function signature is polymorphic and 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. id ::a->a True
False
2. head :: [a] -> a True
False
3. takeWhile :: (Int -> Bool) -> [Int] -> [Int] True
False
4. zipWith ::(a->b->c)->[a]->[b]->[c] True
False
5. fst ::(a,b)->a True
False
Save Answer
Q3 Algebraic Data Type 5 Points
Consider the following algebraic data type:
data Colour = Black | Red | Green | Blue
data Pixel = Pixel Color Color Color
Data type Colour enumerates basic colours Red , Green , Blue whereas Black represents no colour e.g.
Pixel Black Black Red and Pixel Black Red Red represent two shades of Red.
How many colours are encoded by this data type if Pixel ‘s colours are encoded by the number of occurrences of each basic colour (i.e. Red , Green , Blue ) in the data constructor i.e.
Pixel Black Black Red and Pixel Black Red Black encodes the same colour.
# 81 # 32 # 20 # 16 #4
Save Answer
Q4 Typeclasses 2 Points
Consider the following algebraic datatype:
data Weather = Sunny | Cloudy | Rainy
What is the minimum set of type classes the data type Weather should derive from for being able to print its values to the GHCi prompt.
# (Eq, Ord, Show, Read) # (Ord, Show, Read)
# (Ord, Eq, Show)
# (Eq, Show)
# (Show) Save Answer
Q5 Types efficiency 4 Points
For each of the following type declarations consider whether it is suitable to define a rectangle by the four parameters x, y, width, and height such that:
memory overhead should be minimised, values of the type should not be compatible to
(Double,Double,Double,Double) , and
it should be possible to check if two rectangles are equal.
Select True if it is suitable and 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.
type Point = (Double, Double, Double, Double) True
False
type Point = (Double, Double, Double, Double) deriving Eq True
False
data Point = Point (Double, Double, Double, Double) deriving Eq True
False
newtype Point = Point (Double, Double, Double, Double) deriving True
False
Save Answer
Q6
5 Points
For each of the following statements, select whether it is True or False for Haskell.
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.
2.
3.
4.
The length of [“abc”] is 3 . True
False
String is an instance of class Show . True
False
The built-in function zip cannot pair up elements of different types from two lists.
True False
Function application has least priority than all other operators. True
False
f $ g is equivalent to f (g) True
False
Save Answer
Q7 Function signatures 9 Points
Consider the following Haskell function that returns True if a list passed as the first argument is a subset of a list passed as the second argument:
— | isSubset
1.
2.
3.
4.
5.
— Examples:
— >>> isSubset [] [4,5]
— True
— >>> isSubset [1,2] []
— False
— >>> isSubset [] []
— True
— >>> isSubset [4,5] [1,2,3]
— False
— >>> isSubset [1,3] [1,2,3]
— True
—
isSubset [] _ = True
isSubset (x:xs) list
|elem’ x list = isSubset xs list
|otherwise = False
|otherwise = elem’ x ys
Answer the questions below about this function
Q7.1 Polymorphism 5 Points
For each of the following type signatures select True if it is a valid signature for the function isSubset 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.
[a] -> [a] -> Bool True
False
Eqa=>[a]->[a]->Bool True
False
[Int] -> [Int] -> Bool True
False
a->a->Bool True
False
Eqa=>a->b->Bool True
False
Save Answer
Q7.2 Complexity 4 Points
How many comparisons x==y between the elements occur when evaluating isSubset [4,5] [1,2,3] ?
#3 #4 #5 #6 #7
Save Answer
Q8 Precedence 6 Points
Q8.1
3 Points
Which of the following expressions is a rendering of cos2¦È (that is, the square of cos¦È)
1.
2.
3.
4.
5.
cos^2 theta True
False
cos theta^2 True
False
(cos theta)^2 True
False
Save Answer
Q8.2
3 Points
Which of the following expressions is a rendering of sin2¦È 2¦Ð
1. sin(2*theta)/(2*pi) True
False
2. sin(2*theta)/2*pi True
False
3. sin(2*theta/2*pi) True
False
Save Answer
Q9 Complexity 6 Points
data Tree a = Node a [Tree a] deriving (Eq, Show)
1.
2.
3.
elem’ x [] = False
elem’ x (y:ys)
| x == y = True
— | numberOfNodes returns the size of a given tree
— Examples:
—
— >>> numberOfNodes $ Node 5 []
— 1
—
— >>> numberOfNodes $ Node 5 [Node 4 [], Node 3 []]
— 3
—
— >>> numberOfNodes treeExample
— 7
—
numberOfNodes :: Tree a -> Int
numberOfNodes (Node _ children) = 1 + sum (map numberOfNodes chil Answer the questions below about this function
Q9.1 Best case time complexity 3 Points
What is the best case time complexity
# O(1)
# O(log n) # O(n)
# O(nlogn) # O(n2)
Save Answer
Q9.2 Worst case time complexity 3 Points
What is the worst case time complexity
# O(1)
# O(log n) # O(n)
# O(nlogn) # O(n2)
Save Answer
Q10 Complexity 9 Points
data BinTree a = Null | Node a (BinTree a) (BinTree a)
— | takeRightmost
re
— Returns the rightmost element of a binary tree.
—
— Example:
—
— takeRightmost (Node 1 Null (Node 4 (Node 3 (Node 2 Null Null)
— 5
Nul
takeRightmost :: BinTree a -> a
takeRightmost tree = case tree of
Null -> error “Empty list”
Nodex_Null ->x
Node _ _ right -> takeRightmost right Answer the questions below about this function
Q10.1 Best case time complexity 3 Points
What is the best case time complexity if the tree is balanced
# O(1)
# O(log n) # O(n)
# O(nlogn) # O(n2)
Save Answer
Q10.2 Best case time complexity 3 Points
What is the best case time complexity if the tree is unbalanced
# O(1)
# O(log n) # O(n)
# O(nlogn) # O(n2)
Save Answer
Q10.3 Worst case time complexity 3 Points
What is the worst case time complexity if the tree is unbalanced
# O(1)
# O(log n) # O(n)
# O(nlogn) # O(n2)
Save Answer
Q11 Testing 5 Points
When you conduct QuickCheck testing, you
# Test that large number of random inputs obey some property # Test the code to measure speed and memory usage
# Test the whole program, and not individual parts
# Use the form of the actual code to guide your choice to tests
Save Answer
Q12 Lazy evaluation 4 Points
For each of the following statements, select whether it is True or False for Haskell.
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. Given
inf :: Int
inf = 1 + inf
The following expression called by value will never terminate:
fst (0, inf) True
False
2. head fib will result in a non-terminating sequence:
fib = 0:1:[a + b | (a, b) <- zip fib (tail fib)] True
False
3. Lazy evaluation require requires as much space as non-lazy evaluation.
True False
4. The following expression contains two reducible expressions:
fst (4*2, 4+3) True
False
Save Answer
Q13 Haskell Programming 107 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://github.com/Lenskiy/sfe using the command:
git https://github.com/Lenskiy/sfe.git
Q13.1 Lists 16 Points
Click on the link Lists.hs to download a Haskell script containing templates (instructions included) for the functions ntail and
isPal .
If your browser downloads the file as Lists.hs.txt with the
extension .txt make sure to rename the file to Lists.hs . You must complete both the functions ntail and isPal .
Make sure to edit and test using ghci before uploading using the following link:
$ Please select file(s) Select file(s) Save Answer
Q13.2 Shapes 28 Points
Click on the link Shapes.hs to download a Haskell script containing templates (instructions included) for the functions
allEquivalentShapes,findByShape, andisEquivalent.
If your browser downloads the file as Shapes.hs.txt with the
extension .txt make sure to rename the file to Shapes.hs . You must complete both the functions allEquivalentShapes ,
findByShape and isEquivalent .
Make sure to edit and test using ghci before uploading using the
following link:
$ Please select file(s) Select file(s)
Save Answer
Q13.3 DNA 28 Points
Click on the link DNA.hs to download a Haskell script containing templates (instructions included) for the functions partner ,
pairs , pairwiseStrand and binds .
If your browser downloads the file as DNA.hs.txt with the
extension .txt make sure to rename the file to DNA.hs . You must complete both the functions partner , pairs ,
pairwiseStrand and binds .
Make sure to edit and test using ghci before uploading using the
following link:
$ Please select file(s) Select file(s)
Save Answer
Q13.4 Trees 35 Points
Click on the link Trees.hs to download a Haskell script containing templates (instructions included) for the functions isRoot ,
numberOfParents , findInTree , addNodeAtRoot and replaceInTree .
If your browser downloads the file as Trees.hs.txt with the extension .txt make sure to rename the file to Trees.hs .
You must complete both the functions isRoot , numberOfParents , findInTree , addNodeAtRoot and replaceInTree .
Make sure to edit and test using ghci before uploading using the following link:
$ Please select file(s) Select file(s) Save Answer
Save All Answers
Submit & View Submission !
d