Skip to content

Instantly share code, notes, and snippets.

@ijp
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ijp/6bfc61d0cea6005ef1d8 to your computer and use it in GitHub Desktop.
Save ijp/6bfc61d0cea6005ef1d8 to your computer and use it in GitHub Desktop.
;; 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