Search code examples
functional-programmingschemesicpthunk

SICP Checking eval is a thunk?


In Sec 4.2.2 of SICP,

(define (list-of-arg-values exps env)
  (if (no-operands? exps)
      '()
      (cons (actual-value (first-operand exps) env)
            (list-of-arg-values (rest-operands exps)
                                env))))

Calls

(define (actual-value exp env)
  (force-it (eval exp env)))

Calls

(define (force-it obj)
  (if (thunk? obj)
      (actual-value (thunk-exp obj) (thunk-env obj))
      obj))

As per the thunk structure

(define (delay-it exp env)
  (list 'thunk exp env))

(define (thunk? obj)
  (tagged-list? obj 'thunk))

If the first operand was a thunk , why does it check whether the eval is a thunk?

If the operand is a thunk it would be like "list thunk exp `env". but why is it testing if the eval is a thunk?

Can someone please explain how this part works together along with an example


Solution

  • As commented By Sylwester,

    When eval is lazy what eval is returning is always a thunk. When you need to force it it might not be enough to force only one level so it forces as long as the value is a thunk. The second it isn't it is the actual forced value fully evaluated. Imagine the expression (+ a b) needs to evaluate +, a, b before it can actually apply so with lazy evaluation the expression won't actually be run until you force it and then it will evaluate at least 4 things (a and b might be stuff needs forcing too)