It seems to me you might want to curry a function. Imagine you did this:
(defun curry (op arg1) (lambda (&rest args) (apply op (cons arg1 args)))) (funcall (curry #'+ 10) 20) ; ==> 30 (mapcar (curry #'+ 10) '(1 2 3 4)) ; ==> (11 12 13 14)
Now defun makes a function in the global namespace always. It's not like in Scheme where it creates a closure. To do the same as defun we use symbol-function and setf:
(defun create-curried (name op arg1) (setf (symbol-function name) (lambda (&rest args) (apply op (cons arg1 args))))) (create-curried '+x #'+ 10) ; ==> function (+x 20) ; ==> 30 ;; since it's a function, it even works with higher order functions (mapcar create-curried '(+x -x /x *x) (list #'+ #'- #'/ #'*) '(10 10 10 10)) (/x 2) ; ==> 5
Last. With macros you can prettify it.
(defmacro defun-curried (newname oldname arg) (if (and (symbolp newname) (symbolp oldname)) `(create-curried ',newname (function ,oldname) ,arg) (error "Newname and Oldname need to be symbols"))) (defun-curried +xx + 20) (+xx 10) ; ==> 30
oldname is taken from the lexical scope so you may use flet or labels with this but it ends up being global, just like with defun.