留学生辅导 Consider a warehouse that delivers boxes of fruit. Each box contains only

Consider a warehouse that delivers boxes of fruit. Each box contains only one kind of fruit, e.g. a box of 6 apples or a box of 3 bananas, but no mixtures. Haskell data types are constructed to represent fruits and boxes of fruit as follows:
data Fruit = Banana | Apple | Orange | Lychee deriving (Show, Eq) type FruitBox = (Fruit, Int)
So, for example, the tuple (Lychee, 8) is used to represent a box of 8 lychees. Q1. Use these data types to represent a box of 6 oranges and assign it to the
variable name x1. Include the type declaration for x1.

Copyright By PowCoder代写 加微信 powcoder

Q2. Create a list to represent a sequence of 5 fruit boxes in this order: 4 bananas, 3 apples, 5 apples, 1 banana and 3 oranges. Assign this list to variable name xs1. Include the type declaration for xs1.
Q3. Write a function f1 that takes a fruit box, checks if it is a box of 5 or more apples, and returns True if it is, otherwise False. Include the type declaration for f1.
Q4. Use list comprehension to write a function fruitSum that calculates the total number of fruits for a single fruit type given in a list of fruit boxes. For example,
fruitSum Apple [(Banana, 6), (Apple, 3), (Lychee, 4), (Apple, 2)] will return the value 5 (i.e. 3+2 apples). Include the type declaration for fruitSum.
Page 1 of 5

Suppose we wish to implement a decision tree. When applied to some object the decision tree will apply a sequence of conditions and return true or false according to the result. For example, consider the decision tree applied to a fruit box shown in Figure 1.
Fruit is an Apple?
Number of fruits is greater than 3?
Figure 1. Fruit box decision tree
So, e.g. a box of 4 bananas will return false, and a box of 3 apples will return true when this decision tree is applied.
The following data definition defines such decision trees for any generic type of object a:
data DTree a = Leaf Bool | Node (a->Bool) (DTree a) (DTree a)
and here is an implementation of the decision tree shown in Figure 1:
d1 :: DTree FruitBox
d1 = Node (\(f,_)-> f==Apple)
(Node (\(_,n)-> n>3) (Leaf False) (Leaf True))
(Leaf False)
Q5. Use recursion to write a function applyDTree which will apply a decision tree to an object to return a Boolean value. For example,
applyDTree d1 (Banana,4) will return False and
applyDTree d1 (Apple,3) will return True Page 2 of 5

Your function needs to be polymorphic, i.e. it can be used on any type a. Include the type declaration for applyDTree.
Q6. Write code to apply decision tree d1 to x1 and output the Boolean value to variable name b1.
Q7. Write a function mapApplyDTree that maps applyDTree to a list of objects and returns the decisions for the objects as a list. For example,
mapApplyDTree d1 xs1
will return a list of 5 Boolean values (based on xs1 from Q2). Write the body of this
function in one line of code starting with:
mapApplyDTree d = …
Hint: Use currying and the map function.
Q8. Use the DTree data type to construct a new decision tree d2 for boxes of fruit as
shown in Figure 2.
Fruit is banana or orange?
Number of fruits is less than 4?
False True
Figure 2. Decision tree for boxes of fruit (2)
Page 3 of 5

A sequences of integers is an arithmetic progression if the difference between numbers in the sequence is a constant. For example, 2, 5, 8, 11, 14 is an arithmetic progression (the difference is 3), whereas 2, 5, 9, 12, 15 is not (since the difference is either 3 or 4).
Q9. Write a function isArithmeticProgression that takes a list of integers as an argument, given as type [Int] and returns True if and only if it is an arithmetic progression. Include its type declaration.
Note that sequences of 2 or less integers are considered arithmetic progressions.
Hint: you may need to use an auxiliary function to write this function.
Q10. Use DTree to construct a decision tree d3 on lists of integers that represents the decision tree shown in Figure 3.
Hint: applyDTree d3 [2,5,8,11,14] should return True.
The first number in the sequence is even?
The sequence is an arithemetic progression?
False False
The length of the sequence is greater than 2
Figure 3. Decision tree for sequences of integers.
Q11. Use DTree to construct a decision tree d4 that takes a generic decision tree as an argument and returns True if and only if all leaf nodes in the decision tree have the value True. Includethetypedeclarationford4.
Hint: Use an auxiliary function to determine if all the leaf nodes in a decision tree are True.
Page 4 of 5

Q12. Apply d4 to itself and return the result to variable name b4.
Page 5 of 5

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