Exercise 4.1

Force operands to be evaluated from left to right, by using a nested let.
Note that a single let won’t work because (let ((a v1) (b v2))) is just a shortcut for ((lambda (a b) …) v1 v2) and v1 and v2 will use the same order of evaluation as the underlying scheme.

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

Force right to left evaluation. Remember let* is a shortcut for nested lets which I find nicer to read.

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

One thought on “Exercise 4.1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s