CS 3520 Homework 3   - Due September 18

This homework is due September 18, 11:59 PM. Place all of the requested functions in a single file, hw3.scm, and hand it in with submit on the CS filesystem.

Exercise 3.1, Lexical Scope

In each of the following expressions, change the name of all variables so that the binding instances have distinct names, but the meaning of the expression (i.e., the sequence of evaluation steps that produce a value) is not otherwise changed.

  1. (let ([x 1]) (let ([x 2]) x))
  2. (let ([x (let ([x 2]) x)]) x)
  3. (let* ([x 8][x 4]) x)
  4. (let ([x (lambda (x) x)]) (x x))
  5. (letrec ([f (lambda (x) (f x))]) (let ([f (lambda (x) 10)][g (lambda (x) (f x))]) (g 1)))
  6. (letrec ([f (lambda (x) (f x))]) (letrec ([f (lambda (x) 10)][g (lambda (x) (f x))]) (g 1)))
  7. (letrec ([f (lambda (x) (f x))]) (let* ([f (lambda (x) 10)][g (lambda (x) (f x))]) (g 1)))

For each part i, supply your argument as a function answer-for-3.1.i that takes zero arguments and returns a (quoted) S-expression representing the renamed expression.

For example, here is the first answer:

(define (answer-for-3.1.1)
  '(let ([a 1]) (let ([b 2]) b)))

Exercise 3.2, Datatype Definitions

Using the datatype definition in EoPL 3.4 (page 82), convert the following expressions in concrete syntax into abstract syntax values:
  1. 1
  2. x
  3. +(1, 2)
  4. if 0 then x else y
  5. let x = 5 in x
  6. let x = let x = 2 in x in x
For each part i, supply your argument as a function answer-for-3.2.i that takes zero arguments and returns an abstract syntax value. The file hw3.scm contains the relevant define-datatype declarations, so you don't have to type them yourself.

Exercise 3.3, Free Variables

Implement the procedure free-vars, which takes an expression as an abstract syntax value (still using the language from EoPL 3.4 (page 82)) and returns a list of symbols naming the free variables of the expression. A symbol can appear any number of times in the result list, and the order does not matter. The definition of free variables for the language is provided in the lecture slides.

Exercise 3.4, Bound Variables

Implement the procedure bound-vars, which takes an expression as an abstract syntax value (still using the language from EoPL 3.4 (page 82)) and returns a list of symbols naming the bound variables of the expression. A symbol can appear any number of times in the result list, and the order does not matter. The definition of bound variables for the language is provided in the lecture slides.

Hint: The definition of bound variables refers to the definition of free variables. What does that suggest abount your implementation?


Last update: Tuesday, September 11th, 2001
mflatt@cs.utah.edu