Created
June 22, 2012 18:56
-
-
Save osa1/2974513 to your computer and use it in GitHub Desktop.
call/cc in Common Lisp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro defcont (name (&rest params) &body body) | |
(let ((result (gensym))) | |
`(defun ,name (continuation ,@params) | |
(let ((,result ,@body)) | |
(funcall continuation ,result))))) | |
(defun call/cc (continuation fun) | |
(funcall fun continuation)) | |
(defcont multiply (&rest args) | |
(apply #'* args)) | |
(defcont add (&rest args) | |
(apply #'+ args)) | |
;; (* 3 (+ 1 2)) | |
;; (funcall #'add (lambda (r) (funcall #'multiply #'identity r 3)) 1 2) | |
;; (add (lambda (r) (multiply #'identity r 3)) 1 2) | |
(call/cc (lambda (r) (+ r 10)) (lambda (cont) (funcall cont 11))) | |
(call/cc (lambda (r) (+ r 10)) (lambda (cont) 11)) | |
(defvar return nil) | |
(call/cc (lambda (r) (+ 1 r)) | |
(lambda (cont) | |
(setf return cont) | |
1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment