首页 >
> 详细

CP1 1902/159.202

AKLI

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 2 of 5 COS

Question 1. Short Answer Questions [20 marks]

a. Describe the major differences between declarative and imperative [2 marks]

programming languages.

b. Describe the similarities and differences between the logic and functional [2 marks]

programming language paradigms.

c. Describe what referential transparency is and discuss its importance for [2 marks]

functional programming languages.

d. Give two differences between imperative arrays and declarative lists. [2 marks]

e. Briefly discuss the feature of Haskell that allows it to support infinite lists. [2 marks]

f. Briefly describe what higher-order functions are and how they can be used. [2 marks]

g. What is the difference between functions and operators? [2 marks]

h. What are the two main data structures in Haskell? [2 marks]

i. Explain why input/output introduces a problem in functional languages. [2 marks]

j. Briefly describe what a lambda function is and what the advantage of using [2 marks]

one is.

Page 3 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 68 is happy (reached 1)

13 ® 12 + 32 = 10 ® 12 + 02 = 1 13 is happy (reached 1)

24 ® 22 + 42 = 20 ® 22 + 02 = 4 24 is unhappy (reached 4)

a. Write a function to determine whether a number is happy. [2 marks]

example:

Main> happy 13

True

Main> happy 24

False

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. [2 marks]

c. Implement the function using if-then-else statements and recursion. [2 marks]

d. Implement the function using list comprehension(s) and without recursion. [2 marks]

e. Rewrite the function using the built-in function map and without recursion. [2 marks]

Page 4 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. [2 marks]

Main> length [1,2,3,4,5]

5

b. A function to find the maximum value in a list. [2 marks]

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 [2 marks]

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. [3 marks]

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 [3 marks]

and all other letters are lowercase. You are allowed to use the following three

functions in your answer:

isLetter returns true if character is a letter, false otherwise.

toUpper converts a lowercase char to uppercase, unchanged otherwise.

toLower 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 5 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 [2 marks]

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 [2 marks]

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 [2 marks]

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 [4 marks]

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]

+ + + + + + + +

联系我们

- QQ：99515681
- 邮箱：99515681@qq.com
- 工作时间：8:00-23:00
- 微信：codinghelp

- 代写cmpt 214编程、代做programming语言、代写c/C++程序 2020-11-08
- 代写csci 2122课程、代做program编程实验、C++程序语言代写代 2020-11-08
- Fit5032语言编程代做、代写web程序实验、Web、Html程序语言代做 2020-11-08
- Com3503程序编程代做、Java，C++，Python留学生编程代写代写 2020-11-08
- 代写program程序课程、代写c++编程实验、C/C++编程语言代做 代做 2020-11-08
- Data留学生编程代做、代写python程序、Java，C++程序语言代写 2020-11-08
- 代写secj 1023实验编程、Programming程序代做、代写c++语 2020-11-08
- 代写cmpsc 465编程、代做java程序语言、Python，C++编程设 2020-11-07
- 代做mf 703语言编程、代写programming程序、Sql编程语言调试 2020-11-07
- 954246编程设计调试、代做programming程序、C++编程语言代写 2020-11-07
- Pstat 115程序实验代写、R编程语言调试、Data留学生程序代做 代写 2020-11-07
- Com1005课程编程代做、代写python程序、Java，C++程序语言调 2020-11-07
- Tcp留学生程序代写、Java程序设计调试、Java编程语言代写 帮做r语言 2020-11-07
- 代写program语言编程、代做data留学生程序、Python，Java编 2020-11-07
- 代做cosc2666编程、代写programming程序、C/C++程序语言 2020-11-07
- Digital编程设计代写、代做r程序实验、代写r留学生程序 调试matla 2020-11-07
- 代写programming程序实验、R程序语言调试、Data课程编程代做 代 2020-11-07
- Comp104-17B程序代写、代做c++编程实验、C++程序语言调试 帮做 2020-11-07
- Csc8501课程程序代做、C++编程语言调试、代写program编程课程 2020-11-07
- 代写css3留学生程序、代做html、Css编程语言、Data课程程序代做 2020-11-07