Chapter 4, Metalinguistic Abstraction

Exercise 4.6


Only one thing to mention, used fold-right to avoid looping twice.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(define (let? exp) (tagged-list? exp 'let))

(define (let-varexps exp) (cadr exp))

(define (let-body exp) (cddr exp))

(define (let->combination exp)
  (let ((res (fold-right
			  (lambda (new rem)
				(cons (cons (car new) (car rem))
					  (cons (cadr new) (cdr rem))))
			  (cons '() '())
			  (let-varexps exp))))
	(let ((vars (car res))
		  (vexps (cdr res)))
	  (cons (make-lambda vars (let-body exp)) vexps)
	  )))

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
;;; M-Eval input:
(let ((x (cons 1 2)) (y (cons 'a 'b))) 'hello 'world)

;;; M-Eval value:
world

;;; M-Eval input:
(let ((x (cons 1 2)) (y (cons 'a 'b))) (cons x y))

;;; M-Eval value:
((1 . 2) a . b)

;;; M-Eval input:
(let ((x (+ 1 2)) (y 100)) (+ x y))

;;; M-Eval value:
103