SICP Exercise 4.6

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)))

7 Responses to SICP Exercise 4.6

  1. [...] also SICP Exercise 4.6  Subscribe in a [...]

  2. [...] have implemented let in SICP Exercise 4.6. In this exercise, let’s change the syntax of let [...]

  3. [...] SICP Exercise 4.6 for the definitions of let-assignment, let-body, let-var and [...]

  4. [...] SICP Exercise 4.6 for procedures let? and let->combination.  Subscribe in a [...]

  5. ruben says:

    let-exp y let-var could’ve been implemented in terms of map (using car and cadr as its arguments procedure respectively).

  6. pollop says:

    I think
    (define (let-body exp) (cddr exp))
    should be
    (define (let-body exp) (caddr exp))

  7. leon says:

    ;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))))

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.