{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture23

# lecture23 - N ORMAL O RDER AND L AZY E VALUATOR G EORGE...

This preview shows pages 1–3. Sign up to view the full content.

N ORMAL O RDER AND L AZY E VALUATOR 23 G EORGE W ANG [email protected] 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 it’s Louis that this doesn’t 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 book’s version uses cons-stream , which is a special form, and so what looks like a recursive call actually isn’t—at 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
program such as Louis’s 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. Here’s 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 it’s far too horrible to contemplate; with this technique, the stream programmer has to check carefully every procedure to see what might need to be delayed explicitly. This defeats the object of
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 4

lecture23 - N ORMAL O RDER AND L AZY E VALUATOR G EORGE...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online