Assignment 1 – Contracts

Assignment 1 – Contracts

Due Fri 10/1 11:59pm

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

  #lang racket
   
  (module sum racket
   
    (provide
     (contract-out
      [sum any/c]))
   
    (define (sum l)
      (cond
        [(empty? (rest l)) (first l)]
        [else (+ (first l)
                 (sum (rest l)))])))
   
  (require (submod "." sum))

2. Add a submodule and a dependent contract via contract-out that ensures that the results of the function below, f, are positive integers (the contract may 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
   
  (module g 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.

Submit a racket file with your solution via Canvas.