[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Scheme-reports] exception handling

John Cowan writes:
> Per Bothner scripsit:
> > Assuming guard is definable in terms of with-exception-handler,
> > there should be a definition in section 7.3 "Derived expression
> > types".
> There should, indeed, but no one has written one.  Contributions are
> earnestly solicited.

What about the one in <http://srfi.schemers.org/srfi-34/srfi-34.html>?
Cut-and-pasted from there:

(define-syntax guard
  (syntax-rules ()
    ((guard (var clause ...) e1 e2 ...)
       (lambda (guard-k)
          (lambda (condition)
               (lambda (handler-k)
                  (lambda ()
                    (let ((var condition))      ; clauses may SET! var
                      (guard-aux (handler-k (lambda ()
                                              (raise condition)))
                                 clause ...))))))))
          (lambda ()
             (lambda () e1 e2 ...)
             (lambda args
               (guard-k (lambda ()
                          (apply values args)))))))))))))

(define-syntax guard-aux
  (syntax-rules (else =>)
    ((guard-aux reraise (else result1 result2 ...))
     (begin result1 result2 ...))
    ((guard-aux reraise (test => result))
     (let ((temp test))
       (if temp 
           (result temp)
    ((guard-aux reraise (test => result) clause1 clause2 ...)
     (let ((temp test))
       (if temp
           (result temp)
           (guard-aux reraise clause1 clause2 ...))))
    ((guard-aux reraise (test))
    ((guard-aux reraise (test) clause1 clause2 ...)
     (let ((temp test))
       (if temp
           (guard-aux reraise clause1 clause2 ...))))
    ((guard-aux reraise (test result1 result2 ...))
     (if test
         (begin result1 result2 ...)
    ((guard-aux reraise (test result1 result2 ...) clause1 clause2 ...)
     (if test
         (begin result1 result2 ...)
         (guard-aux reraise clause1 clause2 ...)))))

Scheme-reports mailing list