<< Previous exercise (4.5) | Index | Next exercise (4.7) >>
;; without using map (define (let-bindings exp) (cadr exp)) (define (let-body exp) (cddr exp)) (define (bindings->params bindings) (if (null? bindings) bindings (cons (caar bindings) (bindings->params (cdr bindings))))) (define (bindings->args bindings) (if (null? bindings) bindings (cons (cadar bindings) (bindings->args (cdr bindings))))) ;; TODO Check if bindings are pairs (define (let->combination exp) (cons (make-lambda (bindings->params (let-bindings exp)) (let-body exp)) (bindings->args (let-bindings exp))))
This is extremely nitpicky, but I'd prefer to use make-application than cons here, since it maintains the abstraction barrier of the implementation of procedure application.
Hertz
Because the initial values for the vars serve as the remainder of the list (cdr) instead of a separate list, the let->combination should be defined using 'cons' instead of 'list'.
(define (let->combination expr) (cons (make-lambda (let-vars expr) (let-body expr)) (let-inits expr)))
Dolemo
I think let-body above seems to have a bug.
(define (let-body expr) (caddr expr))
because body part is one of three parts of let expression list.
Shi-Chao
Body part may contain several expressions, so 'cddr' is used to point all the rest parts.