Skip to content

Instantly share code, notes, and snippets.

@mishoo
Created September 30, 2013 19:47
Show Gist options
  • Save mishoo/6769144 to your computer and use it in GitHub Desktop.
Save mishoo/6769144 to your computer and use it in GitHub Desktop.
catch/throw in terms of call/cc
(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