chickadee » sicp » with-lazy-lists

with-lazy-lists procedures receive-envprocedure

Sets up an environment where lazy cons, car and cdr have been defined.

With-lazy-lists is a wrapper around with-primitive-procedures.

procedures
A key-value list of names and their primitive procedures in the underlying Scheme.
receive-env
A lambda of one value that takes the prepared environment
(define (with-lazy-lists procedures receive-env)
  (with-primitive-procedures
    (append procedures `((= ,=) (- ,-)))
    (lambda (env)
      (eval* '(define (cons x y) (lambda (m) (m x y))) env)
      (eval* '(define (car z) (z (lambda (p q) p))) env)
      (eval* '(define (cdr z) (z (lambda (p q) q))) env)
      (eval* '(define (list-ref items n)
                (if (= n 0) (car items) (list-ref (cdr items) (- n 1))))
             env)
      (eval* '(define (map proc items)
                (if (null? items)
                  '()
                  (cons (proc (car items)) (map proc (cdr items)))))
             env)
      (receive-env env))))