çä¼¼Continuationã¢ãã
Haskellã®Continuationã¢ãããé°å²æ°ã ãGaucheã«ç§»æ¤ãã¦ã¿ãã
Contã¯ã©ã¹
ã¾ããçä¼¼Continuationã¢ããã¨ãã¦ãContã¯ã©ã¹ãä½ãã
Contã¯ã©ã¹ãæã¤runContã¹ãããã¯ååã®getteré¢æ°ãæã¤ã
returnã¯å
¨ç¶ã¸ã§ããªãã¯ãã¡ã³ã¯ã·ã§ã³ã«ãªã£ã¦ãªããã ãã©ã>>=ã¨æ¨ªä¸¦ã³ã«ãããã£ãã®ã§ã
(define-class <Cont> () ((runCont :init-keyword :r :getter runCont))) (define-method return (a) (make <Cont> :r (lambda (k) (k a)))) (define-method >>= ((c <Cont>) f) (make <Cont> :r (lambda (k) ((runCont c) (lambda (a) ((runCont (f a)) k))))))
対å¿ããHaskellã®ã³ã¼ã
newtype Cont r a = Cont { runCont :: ((a -> r) -> r) } instance Monad (Cont r) where return a = Cont $ \k -> k a c >>= f = Cont $ \k -> runCont c (\a -> runCont (f a) k)
éä¹ã®ãµã³ãã«
ä¸è¨ã使ã£ã¦éä¹ãæ¸ãã¦ã¿ãã
(define (fact n) (if (eq? n 0) (return 1) (>>= (fact (- n 1)) (lambda (a) (return (* n a)))))) (define (main args) ((runCont (fact 4)) print))
対å¿ããHaskellã®ã³ã¼ã(ãã®éæ¸ããdoè¨æ³ã«ããç´åç)
fact 0 = return 1 fact n = (fact $ n - 1) >>= (\a -> return $ n * a) main = runCont (fact 4) print
ã¾ããè¦ããã«runContã¯åãªãContã¯ã©ã¹ã®getterã§ãããªããã¨ããã®ãå®æãããã£ãã ãã