[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: No ending loop occured when using call-with-current-continuaion
From: |
Keith Wright |
Subject: |
Re: No ending loop occured when using call-with-current-continuaion |
Date: |
Fri, 21 Sep 2001 12:07:25 -0400 |
> From: Ying Luo <address@hidden>
>
> Hi,
>
> When I type commands listed below interactively into guile(not using
> "load"), I found there is a loop without ending occured.
>
> (define call/cc call-with-current-continuation)
>
> (define retry #f)
> (define factorial
> (lambda (x)
> (if (= x 0)
> (call/cc (lambda (k) (set! retry k) 1))
> (* x (factorial (- x 1))))))
>
> (begin (display (factorial 4))
> (newline)
> (display (retry 1))
> (newline))
>
>
> Would you please give me some advice? Thanks a lot!
I advise you not to write infinite loops. Your
program is equivalent to:
>> (let loop () (display 24) (newline) (loop))
My version of Guile correctly prints 24 over and
over until I hit ^C, at which time it returns to a
prompt.
To see the loop, note that after the call to Factorial,
Retry is set equal to a continuation from inside the
call to Factorial that occurs in the Begin just _before_
the call to Retry. When Retry is called it finishes
the computation of Factorial, displays it, and calls
Retry.
So don't do that then.
--
-- Keith Wright <address@hidden>
Programmer in Chief, Free Computer Shop <http://www.free-comp-shop.com>
--- Food, Shelter, Source code. ---