Skip to content

Instantly share code, notes, and snippets.

@Liutos
Created April 21, 2012 06:25
Show Gist options
  • Save Liutos/2434760 to your computer and use it in GitHub Desktop.
Save Liutos/2434760 to your computer and use it in GitHub Desktop.
定义能够进行一次自动partial apply的函数的宏及其辅助函数
(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