Last active
August 29, 2015 14:04
-
-
Save ijp/6bfc61d0cea6005ef1d8 to your computer and use it in GitHub Desktop.
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
;; Scheme version of try ... in ... unless ... from the paper | |
;; Exceptional Syntax by Benton and Kennedy | |
(define-syntax try | |
(syntax-rules (in unless) | |
((try (var val) body ... ((tag handler ...) ...)) | |
((catch #t | |
(lambda () (let ((v val)) | |
(lambda (k) (k v)))) | |
(lambda (t . args) | |
(lambda (k) | |
(case t | |
((tag) handler ...) ... | |
(else (apply throw t args)))))) | |
(lambda (var) body ...))))) | |
(define-syntax try2 ;; suggestion from mark_weaver | |
(syntax-rules () | |
((try (var val) body ... ((tag handler ...) ...)) | |
(let/ec k | |
(let ((var | |
(catch #t | |
(lambda () val) | |
(lambda (t . args) | |
(case t | |
((tag) (k (begin handler ...))) ... | |
(else (apply throw t args))))))) | |
body ...))))) | |
(try (file (open-file "/tmp/supersekretdata.txt" "r")) | |
(read file) | |
((system-error "you got a system error, bro"))) | |
;; $3 = "you got a system error, bro" | |
(try (file (open-file "/tmp/try.scm" "r")) | |
(read file) | |
((system-error "you got a system error, bro"))) | |
;; Some sexp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment