Problem
The problem that I see here is that, by the code we can't tell if the value is to be saved as the variable's symbol-value
or symbol-function
. Thus when you put a +
as a value to some corresponding variable, say v
, then it'll always be saved as the symbol-value
of var
, not it's symbol-function
.
Therefore when you'll try to use it as, say (v 1 2)
, it won't work. Because there is no function named v
in the functions' namespace(see this).
So, what to do?
A probable solution can be explicit checking for the value that is to be bound to a variable. If the value is a function, then it should be bound to the variable's function value. This checking can be done via fboundp
.
So, we can make a macro functioner
and a modified version of match-if
. functioner
checks if the value is a function, and sets it aptly. match-if
does the dynamic local bindings, and allows other code in the scope of the bound variables.
(defmacro functioner (var val)
`(if (and (symbolp ',val)
(fboundp ',val))
(setf (symbol-function ',var) #',val)
(setf ,var ,val)))
(defun match-if (pattern input bindings)
(eval `(and (let ,(mapcar #'(lambda (x) (list (car x))) bindings)
(declare (special ,@ (mapcar #'car bindings)))
(loop for i in ',bindings
do (eval `(functioner ,(first i) ,(rest i))))
(eval (second (first ',pattern))))
(pat-match (rest ',pattern) ',input ',bindings))))