[plt-scheme] Stylistic (I hope!) question regarding driver loop

From: Robby Findler (robby at cs.uchicago.edu)
Date: Sat Jan 21 15:24:18 EST 2006

The body of a letrec is in tail-position wrt to the letrec, so you're
set on that score.

(FWIW, You can test this out yourself. This program:

  (define (f x) (letrec ([x x]) (f 1)))
  (f 1)

doesn't accumulate memory, but this one (for example):

  (define (f x) (letrec ([x (f 1)]) x))
  (f 1)

does. Watch out in the second case -- it allocates memory pretty
quickly and gets hard to break pretty quickly too.)

Robby

At Sat, 21 Jan 2006 11:31:24 -0800, Gregory Woodhouse wrote:
> I thought I might try experimenting with the "meta-circular"  
> interpreter in SICP, perhaps implementing lazy evaluation rules or  
> other language modifications. Well, I put together a little driver  
> loop using letrec
> 
> (letrec
>      ((main-loop
>        (lambda ()
>          (begin
>            (display ">> ")
>            (let ((input-exp (read)))
>              (unless (equal? input-exp '(exit))
>                (let ((the-value
>                       (evaluate input-exp)))
>                  (begin
>                    (display the-value)
>                    (newline)))
>                (main-loop))))))
>       (evaluate
>        (lambda (x) x)))
>    (main-loop))
> 
> and it seems to work just fine. My first concern was that a construct  
> like this would chew up stack space, but then I thought it was just  
> tail recursion so it shouldn't matter. But then I wonder if placing a  
> call to main-loop in the body of a letrec like that makes any  
> difference. Is this safe? Is there a better way to write it?
> 
> ===
> Gregory Woodhouse
> gregory.woodhouse at sbcglobal.net
> 
> "All truth passes through three stages: First, it is
> ridiculed. Second, it is violently opposed. Third,
> it is accepted as being self-evident."
> --Arthur Schopenhaur
> 
> 
> 
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme


Posted on the users mailing list.