[plt-scheme] Stylistic (I hope!) question regarding driver loop
Gregory Woodhouse wrote:
>
> On Jan 21, 2006, at 2:06 PM, Gregory Woodhouse wrote:
>
>>> Greg, try using syntax-check on this program in DrScheme:
>>>
>>> (define (f x) (letrec ([x x]) (f 1)))
>>> (f 1)
>>>
>>> (define (g x) (letrec ([x (g 1)]) x))
>>> (g 1)
>>>
>>> Then notice the purple arrow, when you
>>> move the arrow over f.
>>>
>>> Then try the same with g.
>>>
>>> --
>>
>> So...you're saying my code is bad because it invokes a function bound
>> by a letrec in the body of the letrec (like the x in g)?
I was trying to say, that one can see whether a call is
tail recursive or not by studying the arrows in DrScheme.
> I'm sorry, that's a dumb question (on my part). I thought that the
> point of the above is that x occurs after the recursive call to g,
> making it non-tail-recursive. Maybe I missed the point.
In
(define (g x) (letrec ([x (g 1)]) x))
the return value of g is bound to x, and then the value
of the expression x (the last one) is returned. Thus
the call to g is tail recursive.
If you syntax-check your program
(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 position the mouse cursor over the call to main-loop
in the letrec (the one you worried about), you can follow
the purple arrows to see who gets the value returned.
Since the arrows end at the lambda, the call was
tail-recursive.
--
Jens Axel Søgaard