正在加载图片...
3. Conditionals and if 3(define (greater? exp)(tag-check exp ' greater*)) 4 (define (if? exp (tag-check exp 'if*)) (define (eval exp) (cond 8 ((number? exp) exp) ((sum? (eval-sum exp)) ((symbol? exp) (lookup exp)) 01234 (define? )(eval-define exp ((greater? exp)(eval-greater exp)) ((if? exp) (eval-if exp)) (else (error unknown expression exp))) 56789 (define (eval-greater exp) ((eval (cadr exp))(eval (caddr exp)))) 20 (define (eval-if exp) (let ((predicate (cadr exp)) (consequent (caddr exp)) 23456 (alternative (cadddr exp))) fs ((test (eval predicate))) (let ((eq? test #t) (eval consequent) ((eg? test #f) (eval alternative) (else (error "predicate not a conditional: dicate)))))) 31 (eval (define* y* 9)) 5)) 35 i Index to procedures that have not changed 36 procedure page line sum? eval-sum 1 13 lookup define? 2 3 421 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 3. Conditionals and if (define (greater? exp) (tag-check exp 'greater*)) (define (if? exp) (tag-check exp 'if*)) (define (eval exp) (cond ((number? exp) exp) ((sum? exp) (eval-sum exp)) ((symbol? exp) (lookup exp)) ((define? exp) (eval-define exp)) ((greater? exp) (eval-greater exp)) ((if? exp) (eval-if exp)) (else (error "unknown expression " exp)))) (define (eval-greater exp) (> (eval (cadr exp)) (eval (caddr exp)))) (define (eval-if exp) (let ((predicate (cadr exp)) (consequent (caddr exp)) (alternative (cadddr exp))) (let ((test (eval predicate))) (cond ((eq? test #t) (eval consequent)) ((eq? test #f) (eval alternative)) (else (error "predicate not a conditional: " predicate)))))) (eval '(define* y* 9)) (eval '(if* (greater* y* 6) (plus* y* 2) 15)) ; Index to procedures that have not changed: ; procedure page line ; sum? 1 4 ; eval-sum 1 13 ; lookup 2 22 ; define? 2 3 ; eval-define 2 28
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有