程序代写代做代考 Haskell data structure 1702/159.202 ALK Internal

1702/159.202 ALK Internal
CP1
MASSEY UNIVERSITY AUCKLAND CAMPUS
EXAMINATION FOR
159.202 DECLARATIVE PROGRAMMING Semester II – 2017
Time Allowed: THREE (3) hours INSTRUCTIONS
This paper has four questions Answer all questions
This final examination has 55 marks which contributes 55% to the final mark. Students are NOT permitted to remove question papers from the exam room The exam paper will be made available on the Massey University Library website All answers to be written in the blue answer booklet provided
Page 1 of 4

1702/159.202 ALK Internal
Question 1. Short Answer Questions
a. What feature do functional languages use to implement repetition? What are
two the cases of this feature?
b. Describe one similarity and one difference between functional and logic languages.
c. Discuss the difference between a pure and an impure functional language.
d. Compare and contrast the type systems of Haskell and Prolog.
e. Explain why functional languages are able to use lazy evaluation while imperative language cannot.
f. What are the two main data structures in functional languages? Describe the difference between them.
g. Describe the two mechanisms Haskell uses to determine the order in which operators should be applied.
h. What are higher-order functions and how are they used in functional languages?
i. What is a lambda expression and why would you use one?
j. What problem does input/output introduce into functional languages and how does Haskell overcome this problem?
CP1 [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 4

1702/159.202 CP1 ALK
Internal
Question 2. Haskell Programming [10 marks]
Write a function filterPositive that takes a list of integers and returns a list consisting only of the elements that are greater than or equal to 0.
For example:
> filterPositive [1, -3, 2, 0, -17, 12, 84] [1, 2, 0, 12, 84]
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 filter.
Question 3. Haskell Programming – Lists and Polymorphism
[2 marks] [2 marks] [2 marks] [2 marks] [2 marks]
[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 two lists and combines them into a single list of tuples.
d. A function to convert a string to all lower-case characters.
e. A function that implements quicksort using list comprehensions.
[3 marks] [3 marks] [3 marks] [3 marks] [3 marks]
Page 3 of 4

1702/159.202 CP1 ALK
Internal
Question 4. Haskell Programming – Higher-Order Functions [10 marks]
a. Write your own implementation of the higher-order function foldl. This function should
reduce a list down into a single value (left to right) [3 marks]
b. Using a partial application, a lambda expression and the built-in function map, define a
function that squares every value in a list of numbers (should return a list). [3 marks]
c. Write a higher-order function called mapEach that takes a list of functions (that take one argument) and a list of elements and applies each function to the corresponding element in the other list. You can assume the two lists have the same length. An example of this function is shown below: [4 marks]
> mapEach [(+7), (*3), (^3)] [1, 2, 3] [8, 6, 27]
++++++++
Page 4 of 4