This is the mail archive of the
guile@sourceware.cygnus.com
mailing list for the Guile project.
Re: Q: throw and catch
- To: Ivan Toshkov <ivant at ifao dot net>
- Subject: Re: Q: throw and catch
- From: Mikael Djurfeldt <mdj at mdj dot nada dot kth dot se>
- Date: 10 Mar 2000 16:34:12 +0100
- Cc: guile at sourceware dot cygnus dot com
- Cc: djurfeldt at nada dot kth dot se
- References: <20000310165718.A23563@intransco.techno-link.com>
Ivan Toshkov <ivant@ifao.net> writes:
> I am trying to find a realization of throw and catch in Scheme.
> I know about call-with-current-continuation, but couldn't think of a good
> way to hide it.
>
> Can anyone show me an implementation?
Here's one. Note, though, that Guile's catch takes a third handler
argument as well.
(define catch #f)
(define throw #f)
(let ((catch-chain '()))
(define (local-catch tag thunk)
(call-with-current-continuation
(lambda (c)
(let ((local-chain (cons (cons tag c) catch-chain))
(old-chain '()))
(dynamic-wind
(lambda ()
(set! old-chain catch-chain)
(set! catch-chain local-chain))
thunk
(lambda ()
(set! catch-chain old-chain)))))))
(define (local-throw tag value)
(cond ((assq tag catch-chain) => (lambda (entry) ((cdr entry) value)))
(else (error "Uncaught throw"))))
(set! catch local-catch)
(set! throw local-throw))