ruandao / blog

0 stars 0 forks source link

[16.1.4] [sicp 4.4] #139

Closed ruandao closed 8 years ago

ruandao commented 8 years ago
Exercise 4.4.  Recall the definitions of the special forms and and or from chapter 1:

and: The expressions are evaluated from left to right. If any expression evaluates to false, false is returned; any remaining expressions are not evaluated. If all the expressions evaluate to true values, the value of the last expression is returned. If there are no expressions then true is returned.
or: The expressions are evaluated from left to right. If any expression evaluates to a true value, that value is returned; any remaining expressions are not evaluated. If all expressions evaluate to false, or if there are no expressions, then false is returned.
Install and and or as new special forms for the evaluator by defining appropriate syntax procedures and evaluation procedures eval-and and eval-or. Alternatively, show how to implement and and or as derived expressions.
ruandao commented 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)
      ))
   ))
ruandao commented 8 years ago

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