Closed ruandao closed 8 years ago
(load "apply-eval.scm")
((lambda()
(define (first-predicate exp)
(cadr exp))
(define (rest-predicates exp)
(cdr exp))
(define (empty-predicate? exp)
(null? (cdr exp)))
(define (last-predicate? exp)
(empty-predicate? (rest-predicates exp)))
((lambda()
(define (and? exp)
(tagged-list? exp 'and))
(define (eval-and exp env)
(cond
((last-predicate? exp)
(eval (first-predicate exp) env))
(else (and (eval (first-predicate exp) env)
(eval-and (rest-predicates exp) env)))))
(put-type-query! and? 'and)
(put-proc! (list 'eval 'and) eval-and)
))
((lambda()
(define (or? exp)
(tagged-list? exp 'or))
(define (eval-or exp env)
(cond
((last-predicate? exp)
(eval (first-predicate exp) env))
((eval (first-predicate exp) env) true)
(else (eval-or (rest-predicates exp) env))))
(put-type-query! or? 'or)
(put-proc! (list 'eval 'or) eval-or)
))
))
引入 true?
来判断真假
(load "apply-eval.scm")
((lambda()
(define (first-predicate exp)
(cadr exp))
(define (rest-predicates exp)
(cdr exp))
(define (empty-predicate? exp)
(null? (cdr exp)))
(define (last-predicate? exp)
(empty-predicate? (rest-predicates exp)))
((lambda()
(define (and? exp)
(tagged-list? exp 'and))
(define (eval-and exp env)
(cond
((last-predicate? exp)
(true? (eval (first-predicate exp) env)))
(else (and (true? (eval (first-predicate exp) env))
(eval-and (rest-predicates exp) env)))))
(put-type-query! and? 'and)
(put-proc! (list 'eval 'and) eval-and)
))
((lambda()
(define (or? exp)
(tagged-list? exp 'or))
(define (eval-or exp env)
(cond
((last-predicate? exp)
(true? (eval (first-predicate exp) env)))
(else (or (true? (eval (first-predicate exp) env))
(eval-or (rest-predicates exp) env)))))
(put-type-query! or? 'or)
(put-proc! (list 'eval 'or) eval-or)
))
))