Chapter 5, Computing with Register Machines

Exercise 5.41


For finding variable, i just duplicated the book’s code for looking up a variable in environment(runtime).

Initially, i used cons for lexical address but from the examples shown here use list, so i changed my code to reflect that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(define (make-lexical-address frame-num elem-num)
  (list frame-num elem-num))

(define (lex-addr-frame-num lexical-address)
  (car lexical-address))
(define (lex-addr-displacement lexical-address)
  (cadr lexical-address))

(define the-empty-cenv '())

(define (extend-cenv frame base-cenv)
  (cons frame base-cenv))

(define (cenv-first-frame cenv) (car cenv))

(define (enclosing-cenv cenv) (cdr cenv))

(define (find-variable var cenv)
  (define (env-loop frame-num cenv)
	(define (scan displacement vars)
	  (cond ((null? vars)
			 (env-loop (+ frame-num 1)
					   (enclosing-cenv cenv)))
			((eq? var (car vars))
			 (make-lexical-address frame-num
								   displacement))
			(else
			 (scan (+ 1 displacement)
				   (cdr vars)))))
	(if (eq? cenv the-empty-cenv)
		'not-found
		(scan 0 (cenv-first-frame cenv))))
  (env-loop 0 cenv))

Examples:

The above code can work indenpendently without any dependencies. Just trying the examples given in the problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1 ]=> 
(find-variable 'c '((y z) (a b c d e) (x y)))

;Value 15: (1 2)

1 ]=> 
(find-variable 'x '((y z) (a b c d e) (x y)))

;Value 16: (2 0)

1 ]=> 
(find-variable 'w '((y z) (a b c d e) (x y)))

;Value: not-found

1 ]=>