联系方式

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

您当前位置:首页 >> Java编程Java编程

日期:2020-09-24 10:44

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]

+ + + + + + + +


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp