Assignment 1 – Contracts

Assignment 1 – Contracts

Due Wednesday 4/10 11:59pm

1. Add a (non-dependent) contract-out specification for this function and write three calls such that one blames this module, one blames the calling module and one produces a result without any blame.

  #lang racket
   
  (define (len l)
    (cond
      [(empty? (rest l)) 1]
      [else (+ 1 (len (rest l)))]))

2. Add a dependent contract via contract-out that ensures that the results of this function are positive (this contract will disallow some inputs that the function does not crash on, but that is okay – just because something does not crash does not make it work) and that there can never be blame to the module containing f.
  #lang racket
   
  (define (f x y)
    (- x y))

3. The following contract does not capture g’s behavior. Come up with an example call to g that results in a contract violation blaming the following module (which contains g).

  #lang racket
   
  (provide
   (contract-out
    [g (-> (-> natural? natural?)
           natural?
           natural?)]))
   
  (define (g f x)
    (- (f (f x))))

4. Without changing the contract on the x argument or on the result of g (so change only the contract on the f argument), adjust the contract so that g cannot be blamed.

Please submit a racket file with your solution via Canvas.