Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
CP1 1902
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]