fork download
  1. ; replace exceptions with defaults
  2. ; response to chaw
  3.  
  4. (import (rnrs exceptions (6)))
  5.  
  6. (define-syntax try
  7. (syntax-rules (trying)
  8. ((try trying expr default)
  9. (call-with-current-continuation
  10. (lambda (return)
  11. (with-exception-handler
  12. (lambda (x) (return default))
  13. (lambda () expr)))))
  14. ((try) #f)
  15. ((try expr) (try trying expr #f))
  16. ((try expr0 expr1 ...)
  17. (let ((t (try trying expr0 #f)))
  18. (if t t (try expr1 ...))))))
  19.  
  20. (display (try (car '(2)) (begin (display "hello") (newline) 3))) (newline) (newline)
  21.  
  22. (define (make-try/default proc dflt)
  23. (lambda args
  24. (guard (exc (else dflt))
  25. (apply proc args))))
  26.  
  27. (define-syntax try/default
  28. (syntax-rules ()
  29. ((_ dflt body body1 ...)
  30. ((make-try/default (lambda () body body1 ...) dflt)))))
  31.  
  32. (display (try/default (begin (display "hello") (newline) 3) (car '(2)))) (newline)
Success #stdin #stdout 0.08s 9704KB
stdin
Standard input is empty
stdout
2

hello
2