Can getf use equal for comparison instead of eq? (common lisp)
I am wondering if there a 开发者_JAVA技巧way that I can force getf to compare using equal instead of eq? I am using the ccl implementation of common lisp.
No. You have to use a different function; something approximately like this might do what you need:
(defun equal-getf (plist indicator)
(second (member indicator plist :test #'equal)))
Edit
Here's a fixed version that treats the list properly as key/value pairs:
(defun equal-getf (plist indicator)
(loop for key in plist by #'cddr
for value in (rest plist) by #'cddr
when (equal key indicator)
return value))
I don't know if there's a way to "override" the default, see if you can find an impl using (describe 'getf)
or (symbol-plist 'getf)
. A possible semplified implementation could be
(defun mgetf (l v) (if (< (length l) 2) NIL (if (equal (car l) v) (car (cdr l)) (mgetf (nthcdr 2 l) v))))
EDITED : use nthcdr instead of double cdr.
This should do the job. It's not nicely recursive, but uses a straight-forward LOOP application. To allow it ti use an arbitrary equivalence predicate, the route to using an optional argument should be straight-forward.
(defun mgetf (place indicator)
(loop for (key value . rest) on place by #'cddr
if (equal key indicator)
do (return value)))
精彩评论