[racket] missing solution 20.2.4 ex:fancy-contracts-poly

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Mon Sep 8 20:51:35 EDT 2014

On Sep 4, 2014, at 12:54 PM, Daniel Bastos wrote:

> 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).


In this context ITEM and X and Y are variables (placeholders) that stand for sets (classes) of data. 

In the following I have translated the purpose statements for these functions into prototype calls: 

> sort: (listof ITEM) (ITEM ITEM -> boolean) -> (listof ITEM)


(sort a-list-of-Xs compare-Xs) : a-list-of-Xs 
  (same Xs indeed, but rearranged) 

> map: ((listof ITEM) -> X) (listof ITEM) -> (listof X)

(map a-function-from-item-on-list-to-X a-list-of-ITEMs) : a-list-of-Xs 



> project: (listof (listof ITEM)) ((listof ITEM) -> (listof X)) -> (listof X)

(project a-list-of-list-of-ITEMs a-function-from-list-of-ITEMs-to-X) : a-list-of-Xs 


> (*) Comparison with exercise 20.2.2


Abstraction works by taking two concrete examples and replacing matching concrete things with variables. 

20.2.2 asks you to come up with contracts for functions that work on one basic kind of data. Use a distinct kind and then apply the abstraction design recipe. You will find that at least one of the above is wrong. 

-- Matthias



Posted on the users mailing list.