UNB/ CS/ David Bremner/ teaching/ cs4613/ tutorials/ CS4613 Tutorial 11: Even more Practice for Final

You do not need to handin this tutorial to the handin server (it is just for practice) There are no submission tests (or marks for this tutorial). There will be a solution posted, but I suggesting trying the exercises before looking at the solution.

There is skeleton file

Free variables

Complete the function free-vars below that finds all free variables in a given Exp expression. Note that environments here are sets of bound identifiers implemented with hash tables.

(test (free-vars (numE 3) empty-env) empty-env)
(test (free-vars (plusE (varE 'x) (numE 3)) empty-env)
      (extend 'x empty-env))
(test (free-vars (plusE (varE 'x) (numE 3))
                 (extend 'x empty-env)) empty-env)
(test (free-vars (let1E 'x (numE 3) (plusE (varE 'x) (numE 3)))
                 empty-env) empty-env)
(test (free-vars (let1E 'x (numE 3) (plusE (varE 'x) (numE 3)))
                 (extend 'x empty-env)) empty-env)
(test (free-vars (let1E 'x (varE 'y) (plusE (varE 'x) (numE 3)))
                 (extend 'x empty-env)) (extend 'y empty-env))
(test (free-vars (lamE 'x  (plusE (varE 'x) (numE 3)))
                 (extend 'x empty-env)) empty-env)
(test (free-vars (appE (varE 'x) (varE 'x)) empty-env)
      (extend 'x empty-env))


Add a syntax rule for `nand` (negated and) that supports short circuiting. Note that by definition of short-circuiting, code coverage is impossible for the tests themselves.

(define-syntax nand
  (syntax-rules () [(_ arg ...)  ....]))

(test (nand #f) #t)
(test (nand #t (begin (/ 1 0) #t)) #f)
(test (nand #f #t (eq? (/ 1 0) 0)) #f)