# 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.

1. (let ([x 1]) (let ([x 2]) x))
2. (let ([y 2]) (let ([y y]) y))
3. (let ([y 2]) (let* ([y 8][y y]) y)
4. (let ([g (lambda (x) (x x))]) (g g))
5. (letrec ([x (lambda (x) (x x))]) x)
6. (letrec ([f (lambda (x) (f x))]) (let ([f (lambda (x) 10)][g (lambda (x) (f x))]) (g 1)))
7. (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.

1. (let ([x 1]) (let ([y 2]) (+ x y z)))
2. (let ([x x]) (let ([y y]) (+ x y z)))
3. (let ([y 2]) (let ([x y]) z))
4. (let ([z 2]) (let* ([y 8][y y]) z)
5. (letrec ([x (lambda (y) (x y))]) (x z))
6. (let ([f (lambda (x) (g x))][g (lambda (x) (f x))]) (g y))
7. (let* ([f (lambda (x) (g x))][g (lambda (x) (f x))]) (g y))
8. (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. 1
2. x
3. +(1, 2)
4. question withdrawn!
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.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