|
|
|
|
|
by kazinator
13 days ago
|
|
Did you know CL allows keywords to be function names? [1]> (setf (symbol-function :add) (function +))
#<SYSTEM-FUNCTION +>
[2]> (setf (symbol-function :subtract) (function -))
#<SYSTEM-FUNCTION ->
[3]> (setf (symbol-function :multiply) (function *))
#<SYSTEM-FUNCTION *>
[4]> (setf (symbol-function :divide) (function /))
#<SYSTEM-FUNCTION />
[5]> (reduce (lambda (x y) (funcall (car y) x (cadr y))) '((:add 5) (:multiply 3) (:subtract 4)) :initial-value 0)
11
This is actually a small benefit of the two namespaces. Keywords can't be variables because they evaluate to themselves, but that is not relevant to the operator position that does not resolve variables. |
|
But this made me realize there's no way in CL (even with PROGV shenanigans) to temporarily bind the function value of a symbol, unlike the variable one through a simple LET =(
So this turns in this horrible (and incorrect, since this doesn't restore the previous FDEFINITIONs) thing when you want to use this hack "properly":