University of Virginia, Department of Computer Science
CS655: Programming Languages, Spring 2001

Manifest: Thursday 1 February 2001
Assignments
Today Problem Set 1

Readings

No new readings assigned. Re-read the parts of Chapter 4 you didn't understand the first time.

Code

Raw text file: http://www.cs.virginia.edu/~evans/cs655/manifests/lazyeval.scm.

(define (apply proc operand env)
  (if (eq? (car proc) 'primitive)
      ((car (cdr proc)) (force-eval operand env))
      (eval (car (cdr (cdr proc))) 
	    (bind-variable (car (car (cdr proc))) 
			   operand (extend-environment env)))))

(define (has-tag s t) (and (list? s) (eq? (car s) t)))

(define (force-eval expr env)
  (if (or (number? expr) (has-tag expr 'primitive))
      expr
      (if (has-tag expr 'thunk)
	  (force-eval (cadr expr) (caddr expr))
	  (if (symbol? expr) 
	      (force-eval (lookup-variable-value expr env) env)
	      (if (has-tag expr 'lambda)
		  (list 'procedure (car (cdr expr)) (car (cdr (cdr expr))))
		  (force-eval
		   (apply (force-eval (car expr) env) 
			  (make-thunk (car (cdr expr)) env) env)))))))

(define (eval expr env)
  (if (or (number? expr) (has-tag expr 'primitive) (has-tag expr 'thunk))
      expr
      (if (symbol? expr) 
	  (lookup-variable-value expr env)
	  (if (has-tag expr 'lambda)
	      (list 'procedure (car (cdr expr)) (car (cdr (cdr expr))))
	      (apply (force-eval (car expr) env)
		     (make-thunk (car (cdr expr)) env) env)))))

(define (make-thunk expr env)
  (list 'thunk expr env))

I am no lazier now than I was forty years ago, but that is because I reached the limit forty years ago.
You can't go beyond possibility.
Mark Twain in Eruption

Also see: Bertrand Russell, In Praise of Idleness, 1932.


CS 655 University of Virginia
Department of Computer Science
CS 655: Programming Languages
David Evans
evans@virginia.edu