代写代考 COMP0002 4 CONTINUED

Define the following Haskell terms: Pattern matching; lambda term
b. Use list comprehension to compute the smallest square larger than 1,000,000. You may use library functions as needed.
c. Write an infix predicate called divides that takes two whole numbers as argu- ments and returns True if the first number evenly divides into the second.

Copyright By PowCoder代写 加微信 powcoder

E.g. 2 divides 4 == True, 3 divides 4 == False.
Use your predicate and list comprehension together with foldr1 to define a pred- icate called prime that takes an integer and returns True if the number is a prime whole number. E.g. prime 17 == True, prime 16 == False, prime -20 == False.
d. Define foldr1 using recursive function style and make sure that you give the type correctly.
e. Use foldr1 and any other library functions to write a predicate on strings called allLow that checks whether a string contains only lower case letters.
E.g. allLow “Cassiobury” == False.
You may gain an extra mark for writing it in “pointless” style.
[Question 2 cont. on next page]
COMP0002 4 CONTINUED
[Question 2 cont.]
f. Suppose you have written a function called merge that merges two sorted lists
(smallest to largest) into a single sorted list.
E.g. merge [1, 7, 10] [2, 8] == [1, 2, 7, 8, 10]
Use your function merge to write a sorting function mSort that takes an unordered list and returns one that is ordered from smallest to largest. Use recursive function style.
g. Use recursive function style to write a function called insert that inserts an ele- ment into a sorted list (low to high) so that the resulting list is also sorted.
E.g. insert ’f’ “acek” == “acefk”
h. Use insert to write a function called iSort that orders a list from high to low. [2 marks]
i. Define the library function zip.
j. Define the library function unzip.
k. Define a function inverseUnzip that has the property
inverseUnzip $ unzip xs == xs.
inverseUnzip ([1,2], [’a’, ’b’]) == [(1, ’a’), (2, ’b’)]
[2 marks] [Question 2 cont. over page]
COMP0002 5 TURN OVER

[Question 2 cont.]
l. Consider the function div. Its definition is:
div :: Int -> Int -> Int
div n m | n < m = 0 | otherwise = 1 + (div (n - m) m) div has pre condition n ≥ 0 and m > 0.
It has post condition div n m = ⌊n ÷ m⌋, where for a real number x, ⌊x⌋ is the largest integer not greater than x, e.g. ⌊3.3⌋ = 3.
6 END OF PAPER
Define a function divtail that is a version of div that takes an extra argu- ment, given to the function first. Unlike div, divtail is tail recursive.
Define two Haskell predicate functions that can be used with QuickCheck to test the correctness of your divtail implementation, one a weak property of the program and the other a strong one.
Explain why these properties are weak and strong and how QuickCheck uses your two predicates to check correctness.

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com