Gauche:call/ccの使用例

Gauche:call/ccの使用例

call/cc の使用例


call/cc の使用例 1

(define (add x1 x2)
  (call/cc
   (lambda (k)
     (unless (number? x1)
       (k -10000))
     (unless (number? x2)
       (k -20000))
     (+ x1 x2))))

(add 1 2)   ; => 3
(add "a" 2) ; => -10000
(add 1 "b") ; => -20000


call/cc の使用例 2

(define-syntax guard
  (syntax-rules ()
    ((guard (var clause ...) e1 e2 ...)
     ((call/cc
       (lambda (guard-k)
         (with-exception-handler
          (lambda (condition)
            ((call/cc
              (lambda (handler-k)
                (guard-k
                 (lambda ()
                   (let ((var condition))
                     (guard-aux
                      (handler-k
                       (lambda ()
                         (raise-continuable condition)))
                      clause ...))))))))
          (lambda ()
            (call-with-values
                (lambda () e1 e2 ...)
              (lambda args
                (guard-k
                 (lambda ()
                   (apply values args)))))))))))))

hamayama(2018/01/19 14:33:15 UTC)(2018/01/20 11:08:04 UTC)
(2018/01/21 03:03:13 UTC)(2018/01/22 02:08:39 UTC)
(2018/01/23 14:16:20 UTC)(2018/01/25 12:04:49 UTC)
(2018/02/06 12:10:14 UTC)

More ...