Created
September 30, 2013 19:47
-
-
Save mishoo/6769144 to your computer and use it in GitHub Desktop.
catch/throw in terms of call/cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define throw (lambda (tag val) | |
(error "Uncaught throw " tag))) | |
(define (catch* needle thunk) | |
(call/cc (lambda (k) | |
(define prev throw) | |
(fluid-let ((throw (lambda (try val) | |
(if (eq try needle) | |
(k val) | |
(prev try val))))) | |
(thunk))))) | |
(defmacro catch (tag . body) | |
`(catch* ,tag (lambda () ,@body))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment