Can getf use equal for comparison instead of eq? (common lisp)

I am wondering if there is a way I can get getf to compare using equals instead of eq? I am using the ccl implementation of generic lisp.

+5
source share
3 answers

No. You must use another function; something like this can do what you need:

(defun equal-getf (plist indicator)
  (second (member indicator plist :test #'equal)))

Edit

Here's a fixed version that correctly treats the list 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))
+4
source

I donโ€™t know if there is a way to โ€œoverrideโ€ the default value, see if you can find it using (describe 'getf)or (symbol-plist 'getf). A possible semplified implementation might be

(defun mgetf (lv)
  (if (<(length l) 2)
      Nil
    (if (equal (car l) v)
      (car (cdr l)) 
    (mgetf (nthcdr 2 l) v))))

EDITED: nthcdr double cdr.

0

That should do the job. It is not very recursive, but uses the direct LOOP application. To allow ti to use an arbitrary equivalence predicate, the path to using an optional argument must be straightforward.

(defun mgetf (place indicator)
  (loop for (key value . rest) on place by #'cddr
     if (equal key indicator)
     do (return value)))
0
source

All Articles