Skip to content

Instantly share code, notes, and snippets.

@osa1
Created June 22, 2012 18:56
Show Gist options
  • Save osa1/2974513 to your computer and use it in GitHub Desktop.
Save osa1/2974513 to your computer and use it in GitHub Desktop.
call/cc in Common Lisp
(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