# CS 3520 Homework 3 - Due September 12

This homework is due September 12, 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.`(let ([x 1]) (let ([x 2]) x))``(let ([y 2]) (let ([y y]) y))``(let ([y 2]) (let* ([y 8][y y]) y)``(let ([g (lambda (x) (x x))]) (g g))``(letrec ([x (lambda (x) (x x))]) x)``(letrec ([f (lambda (x) (f x))]) (let ([f (lambda (x) 10)][g (lambda (x) (f x))]) (g 1)))``(letrec ([f (lambda (x) (f x))]) (letrec ([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)))

The following is **incorrect**, because `answer-for-3.1.1` is not a function:(define answer-for-3.1.1
'(let ([a 1]) (let ([b 2]) b)))

### Exercise 3.2, Free Variables

For each of the following expressions, identify the set of distinct free variables.`(let ([x 1]) (let ([y 2]) (+ x y z)))``(let ([x x]) (let ([y y]) (+ x y z)))``(let ([y 2]) (let ([x y]) z))``(let ([z 2]) (let* ([y 8][y y]) z)``(letrec ([x (lambda (y) (x y))]) (x z))``(let ([f (lambda (x) (g x))][g (lambda (x) (f x))]) (g y))``(let* ([f (lambda (x) (g x))][g (lambda (x) (f x))]) (g y))``(letrec ([f (lambda (x) (g x))][g (lambda (x) (f x))]) (g y))`

For each part *i*, supply your argument as a **function** `answer-for-3.2.`*i* that takes zero arguments and returns a list of distinct symbols.### Exercise 3.3, Bound Variables

For each of the expressions in the previous exercise, identify the set of distinct bound variables.For each part *i*, supply your argument as a **function** `answer-for-3.3.`*i* that takes zero arguments and returns a list of distinct symbols.### Exercise 3.4, Datatype Definitions

Using the following datatype definition for the abstract syntax of simplified Scheme,(define-datatype expr expr?
(number (val number?))
(id (val symbol?))
(plus (first expr?)
(second expr?))
(let-val (id symbol?)
(val expr?)
(body expr?))
(let-func (id symbol?)
(func-id symbol?)
(func-expr expr?)
(body expr?))
(app (rator symbol?)
(rand expr?)))

convert the following concrete-syntax expressions into representations of abstract syntax values:`1``x``+(1, 2)`*question withdrawn!*`let x = 5 in x``let x = let x = 2 in x in x`

For each part *i*, supply your argument as a function `answer-for-3.4.`*i* that takes zero arguments and returns the reprsentation value. (Return the actual value. Do not return a quoted S-expression that would produce the reprsentation if evaluated.)Include the above `expr` datatype definition in your `hw3.scm`.

Last update: Friday, September 6th, 2002mflatt@cs.utah.edu |