程序代写案例-COM2108
时间:2021-01-20
continued over…
COM2108: Functional Programming
Threshold Test #1, attempt 3.

Answer all questions. You must submit them by 3pm (15:00 UTC/GMT) 26th November 2020
via the link on Blackboard. You should submit a single file in either plain text or PDF.

The questions here assume that you have completed and understand the pre-assignment
tasks (both stages). The topics that you are being tested on are:
• Conventions
• Types
• The standard prelude
• Conditional expressions and guarded expressions
• Pattern matching
• Lambda expressions
• Operator sections
• User-defined types
• Recursion
• List comprehensions

In the questions below, you can assume the following types have been defined:
type Domino = (Int, Int)
type Hand = [Domino]
type Board = [Domino]
data End = L | R deriving (Eq, Show)

1. The following function is a solution to one of the functions that was required for Stage 1
of the pre-assignment tasks:
f (x,y) zs = not (null [ (p,q) | (p,q) <- zs,
(p == x && q == y || (q == x && p == y)) ])

a) Which of the requested functions is it?

b) What is the type definition for this function? (Use types as defined above.)

c) Give a recursive solution to the same function.

2. The following function is a solution to one of the functions that was required for Stage 1
of the pre-assignment tasks:
pp as bs = (filter (\a -> canPlay a L bs) as,
filter (\a -> canPlay a R bs) as)
a) Which of the requested functions is it?

b) What is the type definition for this function? (Use types as defined above.)

c) Give a list comprehension solution to the same function.

End of questions.
3. The following function is meant to implement a simple dominoes player – that simply
plays the first domino in its hand that it can on the current board:
t (r:rs) board
| canPlay r L board = (r,L)
| canPlay r R board = (r,R)
It doesn’t work.

a) It does not need a base case. Why is this? (Hint: it is a particular statement in
the specification.)

b) The function can be completed with the addition of a single line. What is this
line?

c) What is the type definition for this function? (Again, using types specified
above.)

4. The following function is meant to calculate the score for the current board of 3s and 5s
dominoes. Assume that the score function has been implemented correctly.
scoreBoard :: Board -> Int
scoreBoard (left:xs)
= score (pips left L + pips right R)
where
right = last (left:xs)
pips (m,n) end
| m == n = m+n
| end == L = m
| otherwise = n

a) Give an example of a value of the input board where this function would
return the wrong result. How would you fix this?

b) Assume you have correctly fixed the problem in part a). If the line saying
right = last (left:xs)
is replaced by
right = last xs

i. If it was run as:
> scoreBoard [(0,3),(3,6),(6,6)]
what would the result be?

ii. Give an example input that would not produce the right result.


























































































essay、essay代写
essay、essay代写