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.

A key-value list of names and their primitive procedures in the underlying Scheme.
A lambda of one value that takes the prepared environment
(define (with-lazy-lists procedures receive-env)
    (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))))
      (eval* '(define (map proc items)
                (if (null? items)
                  (cons (proc (car items)) (map proc (cdr items)))))
      (receive-env env))))