1801/159.202 AKLI
CP1
MASSEY UNIVERSITY AUCKLAND CAMPUS
EXAMINATION FOR
159.202 DECLARATIVE PROGRAMMING
SEMESTER ONE 2018
_________________________________________________________________________________________________________ Time allowed is THREE (3) Hours.
This paper has FOUR (4) questions
ALL questions should be attempted
Write your answers in the Blue Answer Book provided
Non Programmable Calculators are permitted.
Students may NOT remove any part of this question paper from the exam room.
The exam paper will be made available on the University Library website. This final examination has 55 marks and contributes 55% to your final grade
Page 1 of 5
COS
Question 1. Short Answer Questions
a. Compare and contrast the general approaches of declarative and imperative
programming languages.
b. Describe the similarities and differences between the logic and functional programming language paradigms.
c. Explain the differences between declarative Lists and imperative Arrays.
d. Compare and contrast the type systems of Haskell and Prolog.
e. Explain the difference between eager evaluation and lazy evaluation.
f. What is the difference between a pure and an impure functional programming language?
g. In your own words, describe referential transparency and its importance for functional programming languages.
h. Describe two features of the type system used by Haskell.
i. What is the difference between tuples and lists in Haskell?
j. What is currying?
[20 marks] [2 marks]
[2 marks]
[2 marks] [2 marks] [2 marks] [2 marks]
[2 marks]
[2 marks] [2 marks] [2 marks]
Page 2 of 5
COS
Question 2. Haskell Programming [10 marks]
Write a function squares that takes a whole number and returns a list of all the perfect squares (a number which is the square of an integer) from 1 up to (and possibly including) the given number. You may write additional helper functions if you wish.
For example:
> squares 90
[1, 4, 9, 16, 25, 36, 49, 64, 81]
a. Write down the type of the function.
b. Implement the function using guards and recursion.
c. Implement the function using if-then-else statements and recursion.
d. Implement the function using list comprehension(s) and without recursion.
e. Rewrite the function using the built-in function map and without recursion.
[2 marks]
[2 marks]
[2 marks]
[2 marks]
[2 marks]
Page 3 of 5
COS
Question 3. Haskell Programming – Lists and Polymorphism [15 marks]
For all following functions, write the type signature and implementation of the function. The type of the functions should be as general as possible. Do not use the Haskell built-in functions.
a. A function to calculate the length of a list.
b. A function to calculate the sum of a list of numbers.
c. A function that takes a tuple of two lists and returns a list of tuples. e.g.
> convert ([1,2,3,4],[‘d’,’c’,’b’,’a’])
[(1,’d’), (2,’c’), (3,’b’), (4,’a’)]
d. A function to remove all characters from a string except for letters, digits and spaces. e.g.
> removePunctuation “Hello, how’s it going?”
“Hello hows it going”
e. A function that implements quicksort using list comprehensions.
[3 marks]
[3 marks]
[3 marks]
[3 marks]
[3 marks]
Page 4 of 5
COS
Question 4. Haskell Programming – Higher-Order Functions
a. Write a higher-order function countIf that takes a list and a function and returns the number of elements in the list for which the function returns True. This could be used to count the number of even numbers in a list, e.g.
Main> countIf [2,5,4,3,6,7,11] even
3
b. Use a lambda expression, the built-in function map and a partial application to define a function that applies the following formula to every element in a list of numbers.
𝑥 , 𝑥 𝑖𝑠 𝑒𝑣𝑒𝑛 𝑓(𝑥)=’ 2
3𝑥+1, 𝑥𝑖𝑠𝑜𝑑𝑑
[10 marks] [3 marks]
c. Write a higher-order function called mapNTimes that takes a function (that takes one argument), an integer n and a list of elements. The function should be applied to each element in the list n times. e.g.
[4 marks]
> mapNTimes (+2) 2 [1,2,3,4]
[5,6,7,8]
> mapNTimes (^2) 3 [1,2,3,4]
[1,16,81,256]
++++++++
Page 5 of 5
COS
[3 marks]
(If the element is even, divide it by two and if it is odd, multiply it by 3 and add 1.)