程序代写代做代考 Haskell data structure 1902/159.202 AKLI

1902/159.202 AKLI
CP1
MASSEY UNIVERSITY AUCKLAND CAMPUS
EXAMINATION FOR
159.202 DECLARATIVE PROGRAMMING
SEMESTER TWO 2019
_________________________________________________________________________________________________________ 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 only are permitted.
Students may NOT remove any part of this exam 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. Describe the major differences between declarative and imperative
programming languages.
b. Describe the similarities and differences between the logic and functional programming language paradigms.
c. Describe what referential transparency is and discuss its importance for functional programming languages.
d. Give two differences between imperative arrays and declarative lists.
e. Briefly discuss the feature of Haskell that allows it to support infinite lists.
f. Briefly describe what higher-order functions are and how they can be used.
g. What is the difference between functions and operators?
h. What are the two main data structures in Haskell?
i. Explain why input/output introduces a problem in functional languages.
j. Briefly describe what a lambda function is and what the advantage of using one is.
[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]
A happy number is defined as follows – take any positive integer and replace it by the sum of the squares of its digits (in base ten), repeat this process until it reaches 1 (where it stays forever) or it reaches 4 (the process will always reach one of these two numbers). If the process reaches 1 then the number is happy, if it reaches 4 then it is unhappy.
For example:
68®62 +82=100®12 +02 +02 =1 13®12 +32=10®12 +02 =1 24®22 +42=20®22 +02 =4
68ishappy(reached1) 13ishappy(reached1) 24isunhappy(reached4)
a. Write a function to determine whether a number is happy.
example:
Main> happy 13
True
Main> happy 24
False
[2 marks]
Using the function you have written in part a, write a function that takes two positive integers and returns a list of all the happy numbers between these two values (possibly including the two numbers if they are happy) according to the requirements below:
example:
Main> happy_numbers 10 30
[10, 13, 19, 23, 28]
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]
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 (unless explicitly stated). Example expected behaviour is shown for some functions.
a. A function to find the length of a list. Main> length [1,2,3,4,5]
5
b. A function to find the maximum value in a list. Main> maximum [1,4,6,2,13,5]
13
c. A function to find the element in the middle of a list. If there are an even number of elements, return the first element of the pair in the middle.
Main> middle [1,2,3,4,5]
3
Main> middle [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
‘c’
d. A function to find the average of a list of numbers. Main> average [7,9,14,10,12,11]
10.5
e. A function that formats a string so the first letter of every word is uppercase and all other letters are lowercase. You are allowed to use the following three functions in your answer:
[2 marks]
[2 marks]
[2 marks]
[3 marks]
[3 marks]
isLetter
toUpper
toLower
returns true if character is a letter, false otherwise.
converts a lowercase char to uppercase, unchanged otherwise. converts an uppercase char to lowercase, unchanged otherwise.
Main> format “hello CLASS, hOw are YOu goiNG?” “Hello Class, How Are You Going?”
f. A function that implements quicksort using list comprehensions. [3 marks]
Page 4 of 5
COS

Question 4. Haskell Programming – Higher-Order Functions [10 marks]
a. Write a higher-order function foldr1If that takes a list and two functions and folds (combines) selected elements in the list together. The first function should
be used to combine two elements together and the second used to decide whether the elements should be selected.
example:
Main> foldr1If [2,5,4,3,6,7,11] (+) even
12
Main> foldr1If [3,4,5,6,7,8] (*) odd
105
b. Write the higher-order function foldr1If2 that has the same functionality as foldr1If from part a but is implemented using the higher-order functions foldr and filter.
c. Write the higher-order function foldr1If3 that again behaves the same as foldr1If but is written using partial definition(s) and function composition(s).
d. Write a higher-order function called mapEach that takes a list of functions and a list of elements and applies each function to the corresponding element in the other list.
example:
Main> mapEach [sqrt, cos, (\x -> 2**x), (*3)] [4, 0, 3, 5] [2.0, 1.0, 8.0, 15.0]
++++++++
[2 marks]
Page 5 of 5
COS
[2 marks]
[2 marks]
[4 marks]