University of Virginia, Department of Computer Science
CS200: Computer Science, Spring 2003

Notes: Friday 28 March 2003
Schedule

Implementing Environments
(define (make-new-environment frame env)
   (cons frame env))
(define (first-frame env) 
   (car env))
(define (enclosing-environment env) 
   (cdr env))

(define (extend-environment names values env)
  (make-new-environment
   (map (lambda (name value) (cons name value)) 
            names values)
   env))

(define (environment-lookup-name name env)
  (if (null? env) (error "No binding " name)
      (if (frame-contains? name (first-frame env))
          (frame-lookup-name name (first-frame env))
          (environment-lookup-name 
              name (enclosing-environment env)))))

(define (frame-lookup-name name frame)
  (if (null? frame) (error "Name not found:" name)
      (if (eq? (car (car frame)) name)
          (cdr (car frame))
          (frame-lookup-name name (cdr frame)))))
Lazy Evaluator

Are there procedures we can define in a language with lazy evaluation, that cannot be defined in a language with eager evaluation?






Why must begin be a special form in normal Scheme, but can be implemented as a procedure in Lazy Scheme?






Code for lazy evaluator: lazeval.ss

(define (thunk? expr) (tagged-list? expr 'thunk))
(define (make-thunk expr env) (list 'thunk expr env))
(define (thunk-expr thunk) (cadr thunk))
(define (thunk-env thunk) (caddr thunk))

(define (frame-lookup-name name frame)
  (if (null? frame) (error "Name not found in frame:" name)
      (if (eq? (car (car frame)) name) 
	  (car frame) ;;; was (cdr (car frame))
	  (frame-lookup-name name (cdr frame)))))

(define (environment-get-value! name env)
  (if (null? env) (error "No binding for" name)
      (if (frame-contains? name (first-frame env))
	  (let ((np (frame-lookup-name name (first-frame env))))
	    (if (thunk? (cdr np))
		(set-cdr! np (lazeval (thunk-expr (cdr np)) (thunk-env (cdr np)))))
	    (cdr np))
	  (environment-get-value! name (enclosing-environment env)))))

(define (lazapply procedure operands env)  
  (cond 
   ((primitive-procedure? procedure) 
    (lazapply-primitive procedure (map (lambda (op) (lazeval op env)) operands)))
   ((compound-procedure? procedure) 
    (lazeval-sequence
     (procedure-body procedure)
     (extend-environment
      (procedure-parameters procedure)
      (map (lambda (op) (make-thunk op env)) operands)
      (procedure-environment procedure))))
   (else (error "Unknown applicator: " procedure))))

(define (lazeval expr env)
  (cond
   ((self-evaluating? expr) expr)
   ((variable? expr)        (environment-get-value! expr env))
   ((lambda? expr)          (make-procedure (lambda-parameters expr) (lambda-body expr) env))
   ((definition? expr)      (define-variable! 
			      (definition-variable expr)
			      (lazeval (definition-value expr) env) env))
   ((application? expr)     (lazapply (lazeval (application-operator expr) env) 
				      (application-operands expr) env))
   (else                    (error "Unknown expression: " exp))))

I am no lazier now than I was forty years ago, but that is because I reached the limit forty years ago.
Mark Twain in Eruption

... the universities are supposed to provide, in a more systematic way, what the leisure class provided accidentally and as a by-product. This is a great improvement, but it has certain drawbacks. University life is so different from life in the world at large that men who live in academic milieu tend to be unaware of the preoccupations and problems of ordinary men and women; moreover their ways of expressing themselves are usually such as to rob their opinions of the influence that they ought to have upon the general public. Another disadvantage is that in universities studies are organized, and the man who thinks of some original line of research is likely to be discouraged. Academic institutions, therefore, useful as they are, are not adequate guardians of the interests of civilization in a world where everyone outside their walls is too busy for unutilitarian pursuits.

Excerpted from Bertrand Russel, In Praise of Idleness

CS 200


CS 200: Computer Science
Department of Computer Science
University of Virginia

Circle Fractal by Ramsey Arnaoot and Qi Wang

cs200-staff@cs.virginia.edu
Using these Materials