SICP Exercise 4.1

1241 ワード

SICP Exercise 4.1
1、let式で評価の順序を制御します.
(define (list-of-values-left-to-right exps env)
  (if (no-operands? exps)
      '()
      (let ((first (eval (first-operand exps) env)))
        (cons first (list-of-values (rest-operands exps) env)))))

(define (list-of-values-right-to-left exps env)
  (if (no-operands? exps)
      '()
      (let ((rest (list-of-values (rest-operands exps) env)))
        (cons (eval (first-operand exps) env)
              rest))))

次は、この方法のテストです.
(define (foo)
  (cons (begin (display 'left)
               'l)
        (begin (display 'right)
               'r)))

(define (foo-left)
  (let ((a (begin (display 'left)
                  'l)))
    (cons a (begin (display 'right)
                   'r))))

(define (foo-right)
  (let ((b (begin (display 'right)
                  'r)))
    (cons (begin (display 'left)
                 'l)
          b)))

2、また、実行順序を順次評価して制御することもできる.
(define (foo-right)
  (define a (begin (display 'right)
                   'r))
  (cons (begin (display 'left)
               'l)
        a))