Professional Documents
Culture Documents
Ex 6
Ex 6
infixr 6 :+
data Super natural = O | !natural :+ Super natural
a) Implement a function
computing the number of entries (width) and the largest entry (height) in the partition.
Example:
width (1 :+ 3 :+ 5 :+ 1 :+ 4 :+ 2 :+ 1 :+ O) = 7
height (1 :+ 3 :+ 5 :+ 1 :+ 4 :+ 2 :+ 1 :+ O) = 5
1
c) Implement addition and multiplication for super-naturals:
Try to find a good balance between the width of the result and lazyness of the compu-
tation: The result should not be wider than the widest argument. Only when the kth
part of a result is demanded any kth part of an argument should be evaluated.
Make sure to maintain the invariant that the partitions contain positive numbers only.
d) Implement comparison for super-naturals:
compare :: (Num natural , Ord natural ) ⇒ Super natural → Super natural → Ordering
e) Why is the restriction to partitions of positive whole numbers important for your
implementation?