SICP Exercise 4.6
(define (eval exp env)
((let? exp) (eval (let->combination exp) env))
)
(define (let? exp) (tagged-list? exp 'let))
(define (let-assignment exp) (cadr exp))
(define (let-body exp) (cddr exp))
(define (let-exp assignment)
(if (null? assignment)
'()
(cons (cadr (car assignment))
(let-exp (cdr assignment)))))
(define (let-var assignment)
(if (null? assignment)
'()
(cons (car (car assignment))
(let-var (cdr assignment)))))
(define (let->combination exp)
(transform-let (let-assignment exp) (let-body exp)))
(define (transform-let assignment body)
(cons (make-lambda (let-var assignment) body)
(let-exp assignment)))
Like this: Be the first to like this post.
This entry was posted on Monday, October 12th, 2009 at 11:58 pm and is filed under Scheme , SICP . You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response , or trackback from your own site.
Post navigation
« Previous Post
Next Post »
[...] also SICP Exercise 4.6 Subscribe in a [...]
[...] have implemented let in SICP Exercise 4.6. In this exercise, let’s change the syntax of let [...]
[...] SICP Exercise 4.6 for the definitions of let-assignment, let-body, let-var and [...]
[...] SICP Exercise 4.6 for procedures let? and let->combination. Subscribe in a [...]
let-exp y let-var could’ve been implemented in terms of map (using car and cadr as its arguments procedure respectively).
I think
(define (let-body exp) (cddr exp))
should be
(define (let-body exp) (caddr exp))
;if allowing using higher-order procedures, the solution can be more succinct:
(define (let-clauses exp) (cdr exp))
(define (let-parameters exp) (cadr exp))
(define (let-body exp) (cddr exp))
(define (let->combination exp)
(cons
(cons ‘lambda
(cons (map car (let-parameters exp))
(let-body exp)))
(map cadr (let-parameters exp))))