CS3510: Functional Programming and Applications

BSc EXAMINATION 2022
For Internal Students of
Royal Holloway
DO NOT TURN OVER UNTIL TOLD TO BEGIN
CS3510: Functional Programming and Applications
CS3510R: Functional Programming and Applications โ€“
for FIRSTSIT/RESIT CANDIDATES
Time Allowed: TWO hours
Please answer ALL questions
Royal Holloway, University of London 2022
2021/22 Page 1 of 5 NEXT PAGE
Important Copyright Notice
This exam paper has been made available in electronic form
strictly for the educational benefit of current Royal Holloway students
on the course of study in question.
No further copying, distribution or publication of this exam paper is permitted.
By printing or downloading this exam paper, you are consenting to these restrictions.
CS3510/CS3510R
1. (a) What is a curried function Give an example in Haskell to explain your answer. [5 marks]
(b) Consider the following constants si (i=1, โ€ฆ, 5) in Haskell:
s1 = 1 : [2,3,4,5]
s2 = take 4 s1
s3 = head s2
s4 = [ x | x <- s2, x < 3 ] s5 = drop 1 s4 ++ take 1 s4 What are the values of s1, s2, s3, s4 and s5 [5 marks] (c) The type of myHead is supposed to be [a] -> a. Consider the following
incorrect definitions of myHead (their types are different from [a] -> a):
i. myHead xs = xs
ii. myHead (x:xs) = xs
iii. myHead xs = 0
iv. myHead x xs = x
v. myHead xs = xs + 2
What is the type of myHead for each of the above definitions Explain your
answers. [10 marks]
(d) Use pattern matching to define hd :: a -> [a] -> a so that hd x xs returns the first element of xs if xs is non-empty and the value x if xs is empty.
[5 marks]
Page 2 of 5 NEXT PAGE
No further copying, distribution or publication of this exam paper is permitted.
By printing or downloading this exam paper, you are consenting to these restrictions.
CS3510/CS3510R
2. (a) The factorial function f ac is defined as follows: for any non-negative integer
n,
f ac(n) = ( 1, if n = 0
n ร— f ac(n 1) if n โ‰ฅ 1
i. Write a recursive function fac :: Int -> Int in Haskell to implement
the factorial function. [5 marks]
ii. Write a non-recursive method in an imperative language such as Java
to implement the factorial function. (It is OK for you to use pseudo-code
to describe this program.) [5 marks]
(b) Describe an important feature of functional programming that is different
from imperative programming. Use an example to explain your answer.
[5 marks]
(c) Why can a lazy programming language like Haskell deal wi

๐Ÿ’ก Buy the answer for only $12 Get it now โ†’

th infinite objects
Define a Haskell function that takes an infinite list as an input argument and
use it to explain your answer. [10 marks]
Page 3 of 5 NEXT PAGE
No further copying, distribution or publication of this exam paper is permitted.
By printing or downloading this exam paper, you are consenting to these restrictions.
CS3510/CS3510R
3. (a) What is a polyglot programming environment Explain your answer. [5 marks]
(b) What is a multi-paradigm programming language Are they better than tra_x005f ditional (single-paradigm) programming languages Explain your answers.
[5 marks]
(c) The following Scala function is defined by means of for-comprehension:
def f (n:Int) = for (x <- List.range(1,n+1) if n % x == 0) yield x i. Explain informally the meaning of f(n). [3 marks] ii. What is the value of the expression f(10) [2 marks] (d) i. Are Scala and Java the same when == is used to compare two values of primitive types Give an example to explain your answer. [4 marks] ii. Are Scala and Java the same when == is used to compare two objects Give an example to explain your answer. [6 marks] Page 4 of 5 NEXT PAGE No further copying, distribution or publication of this exam paper is permitted. By printing or downloading this exam paper, you are consenting to these restrictions. CS3510/CS3510R 4. (a) What is mutual recursion Define two Haskell functions by means of mutual recursion and use this as an example to explain your answer. [10 marks] (b) Let isLower :: Char -> Bool be a predicate that returns True for lower case characters and False otherwise. Use isLower and list comprehension
to define a function
lowers :: String -> Int
that counts the lower-case letters in a string. For instance, the value of
lowers โ€œHaskellโ€ is 6. [5 marks]
(c) Consider the following data type in Haskell:
data Tree a = Leaf a
| Node (Tree a) (Tree a)
i. An element of the above data type is a binary tree. Describe informally
how the nodes of a tree in the above type are labeled. [2 marks]
ii. Let IntTree be defined as follows:
type IntTree = Tree Int
Define a function (or a binary predicate)
occurs :: Int -> IntTree -> Bool
that decides if a given integer occurs in a binary tree of type IntTree.
[8 marks]
END
Page 5 of 5 ZHL
No further copying, distribution or publication of this exam paper is permitted.
By printing or downloading this exam paper, you are consenting to these restrictions.

๐Ÿ’ก Buy the answer for only $12 Get it now โ†’