Created
April 21, 2012 06:25
-
-
Save Liutos/2434760 to your computer and use it in GitHub Desktop.
定义能够进行一次自动partial apply的函数的宏及其辅助函数
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
(defun cons-lambda-expr (fn arglist rest-args) | |
(eval `(lambda ,rest-args | |
(apply ,fn (append ',arglist (list ,@rest-args)))))) | |
(defmacro defun/partial (name arglist &body body) | |
(let ((args (gensym)) | |
(func (gensym)) | |
(argc (length arglist)) | |
(arity (gensym)) | |
(rest-args (gensym))) | |
`(defun ,name (&rest ,args) | |
(labels ((,func ,arglist | |
,@body)) | |
(let ((,arity (length ,args))) | |
(cond ((= 0 ,arity) | |
#',func) | |
((< ,arity ,argc) | |
(let ((,rest-args (nthcdr (- ,argc ,arity) ',arglist))) | |
(cons-lambda-expr #',func ,args ,rest-args))) | |
(t (apply #',func ,args)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment