Skip to content

Instantly share code, notes, and snippets.

@Liutos
Created July 13, 2012 04:28
Show Gist options
  • Save Liutos/3102720 to your computer and use it in GitHub Desktop.
Save Liutos/3102720 to your computer and use it in GitHub Desktop.
转换普通代码为CPS代码
(defun multinit-list (len init-fn)
(loop :for i :from 1 :upto len :collect (funcall init-fn)))
(defun split-funcall (form)
(let ((args (rest form)))
(values args
(let ((argv (multinit-list (length args) #'gensym)))
`(lambda ,argv ,(cons (first form) argv))))))
(defun append1 (list obj)
(append list (list obj)))
(defun cont-trans (form &optional cont)
(if (every #'atom form)
(append1 form cont)
(multiple-value-bind (args k) (split-funcall form)
(reduce #'(lambda (expr acc)
(cont-trans expr acc))
args :from-end t :initial-value (append1 k cont)))))
(defun continuation-transform (form)
(cont-trans form 'k))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment