[racket] missing solution 20.2.4 ex:fancy-contracts-poly
A candidate for a solution. (I'm not sure I'm correct regarding the
function project. The description says "a lists of lists" (which I
translate to (listof ITEM)) and "a function from lists to Xs", so I
wonder if the domain of this function-argument must be (listof ITEM)
or it could be more general such as (listof Y). I decided to imagine
that the domain of the function-argument would be items of the "list
of lists" and so I made it into (listof ITEM).
Thank you for your attention.
Exercise 20.2.4. Formulate general contracts for
the following functions:
1. sort, which consumes a list of items and a
function that consumes two items (from the list)
and produces a boolean; it produces a list of items.
2. map, which consumes a function from list items
to Xs and a list; it produces a list of Xs.
3. project, which consumes a list of lists and a
function from lists to Xs; it produces a list of Xs.
Compare with exercise 20.2.2.
Solution.
sort: (listof ITEM) (ITEM ITEM -> boolean) -> (listof ITEM)
map: ((listof ITEM) -> X) (listof ITEM) -> (listof X)
project: (listof (listof ITEM)) ((listof ITEM) -> (listof X)) -> (listof X)
(*) Comparison with exercise 20.2.2
For sort, we replaced number with X. So we generalized the type
of the list which we sort. The new procedure can sort a generic list.
For map, we generalized both the domain and co-domain of
the function consumed by map. Before, the consumed function
was only a mapping from numbers to numbers and now it is
a mapping from a generic domain ITEM to a generic co-domain X.
For project, we generalized the type of the list of lists. It was of
type symbol and now it can be a list of lists of any kind of item.