This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: N ORMAL O RDER AND L AZY E VALUATOR 23 GEORGE WANG email@example.com Department of Electrical Engineering and Computer Sciences University of California, Berkeley August 2, 2010 To load the lazy metacircular evaluator, say (load "~cs61a/lib/lazy.scm") 1 Introduction & Motivation To make streams of pairs, the text uses this procedure: ;;;;; In file cs61a/lectures/4.2/pairs.scm (define (pairs s t) (cons-stream (list (stream-car s) (stream-car t)) ;blue (interleave (stream-map (lambda (x) (list (stream-car s) x)) ;yellow (stream-cdr t)) (pairs (stream-cdr s) (stream-cdr t))))) ;green In Exercise 3.68, Louis Reasoner suggests this simpler version: (define (pairs s t) (interleave (stream-map (lambda (x) (list (stream-car s) x)) t) (pairs (stream-cdr s) (stream-cdr t)))) Of course you know because its Louis that this doesnt work. But why not? The answer is that interleave is an ordinary procedure, so its arguments are evaluated right away, including the recursive call. So there is an infinite recursion before any pairs are generated. The books version uses cons-stream , which is a special form, and so what looks like a recursive call actually isntat least not right away. But in principle, Louis is right! His procedure does correctly specify what the desired result should contain. It fails because of a detail in the implementation of streams. In a perfect world, a mathematically correct 1 program such as Louiss version ought to work on the computer. In section 3.5.4, they solve a similar problem by making the stream programmer use explicit delay invo- cations. Heres how Louis could use that technique: (define (pairs s t) (INTERLEAVE-DELAYED (stream-map (lambda (x) (list (stream-car s) x)) t) (DELAY (pairs (stream-cdr s) (stream-cdr t))))) (define (interleave-delayed s1 delayed-s2) (if (stream-null? s1) (force delayed-s2) (cons-stream (stream-car s1) (interleave-delayed (force delayed-s2) (delay (stream-cdr s1)))))) This works, but its far too horrible to contemplate; with this technique, the stream programmer has to...
View Full Document