[plt-scheme] Re: plt-scheme Digest, Vol 7, Issue 31

From: Felix Klock's PLT scheme proxy (pltscheme at pnkfx.org)
Date: Wed Mar 15 12:29:20 EST 2006

Dan-

On Mar 15, 2006, at 10:55 AM, geb a wrote:
> The following code is taken from SICP's section on
> streams verbatim.  While I realize there is an
> infinite recursion generating a stream of numbers,
> apparently the authors felt the code would work but
> drscheme seems to evaluate each infinite stream first.
>  Does this have something to do with lazy evaluation
> (a term I know nothing about?).  Is there some
> underlying reason for this that I don't know about or
> have I made some mistake with the code.

Which part is taken verbatim?  I can see where STREAM-REF is given in  
SICP (scrolling down from the link below), and it does match the  
definition that you give.
[[ http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-24.html#% 
25_sec_3.5.1 ]]
But where do you see their definitions for CONS-STREAM or DELAY?

My understanding is that you can't make CONS-STREAM or DELAY using  
the DEFINE special form, the way you did below, because that will  
define a call-by-value procedure (you say that you know nothing about  
lazy evaluation, so I'll assume you don't know what I mean by call-by- 
value).  In essence, using DEFINE to define CONS-STREAM or DELAY  
defeats their purpose.

If you read carefully, the text says that CONS-STREAM and DELAY are  
both "special forms"; that means that they cannot be defined in the  
same manner as procedures (for the same reason that you can't define  
COND or AND using procedure definition).  It carefully says that  
"Cons-stream is a special form defined so that (cons-stream <a> <b>)  
is equivalent to (cons <a> (delay <b>))"; but it does *not* give a  
statement of the form "(define (cons-stream <a> <b>) ...)", because  
there is not way to do that.  [[ When they say X is equivalent to Y,  
they mean that when you see X in the program text, you can replace it  
with Y; but it does not mean that we have (define X Y) written down  
somewhere. ]]

You could try something like:

(define-syntax cons-stream
     (syntax-rules ()
         ((cons-stream <a> <b>)
          (cons <a> (delay <b>)))))

to define the cons-stream special form.  The delay special form  
should already be built in; if it isn't, you should be able to use  
DEFINE-SYNTAX in a similar manner to the above to get the effect you  
want.

-Felix



Posted on the users mailing list.