乐正

Actions speak louder than words.

Sicp-ex4-6

问题

let表达式也是一种派生表达式,因为:

1
2
(let ((<val1> <exp1>) ... (<valn> <expn>))
     body)

等价于

1
2
3
4
5
((lambda (<va1> ... <valn>)
  <body>)
  <exp1>
  ...
  <expn>)

请实现语法变换过程let->combination,它能将对let表达式的求值归约到对于上面类型组合式的求值。请给eval增加合适的子句以处理let表达式。

解答

eval子句中一条分支判断:

1
((let? exp) (let->combination exp env))
练习4.6 (ex4-6.scm) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(define (let? exp)
  (tagged-list? exp 'let))

(define (let->combination exp env)
  (apply (make-lambda (let-params exp)
                      (let-body exp))
         (let-exps exp)))

(define (let-params exp)
  (map car (cadr exp)))

(define (let-exps exp)
  (map cadr (cadr exp)))

(define (let-body exp)
  (caddr exp))

draft

« sicp-ex4-5 Python 中的关键字with详解 »

Comments