; replace exceptions with defaults

(import (rnrs exceptions (6)))

(define-syntax try
  (syntax-rules (trying)
    ((try trying expr default)
      (call-with-current-continuation
        (lambda (return)
          (with-exception-handler
            (lambda (x) (return default))
            (lambda () expr)))))
    ((try) #f)
    ((try expr) (try trying expr #f))
    ((try expr0 expr1 ...)
      (let ((t (try trying expr0 #f)))
        (if t t (try expr1 ...))))))

(display (try (car '()) #f)) (newline)
(define a-list '((1 . 1) (3 . 3) (5 . 5)))
(define (lookup x) (try (cdr (assoc x a-list)) x))
(display (lookup 4)) (newline)
(display (try (car '()) (+ 2 2))) (newline)