Generic Lisp how to click on a returned list?

Suppose I have a list variable *test*set to(:v1 (1) :v2 (2))

And then, after some string parsing, I will need to add another one 1to :v1, which is equivalent to:

(push 1 (getf *test* :v1))

However, this will look more:

(push 1 (getf-string-equal *test* "v1"))

Where is getf-string equal (taken from here )

(defun getf-string-equal (plist indicator)
  (loop
     for (i v) on plist by #'cddr
     when (string-equal i indicator)
     return v))

My problem, however, is that I cannot use setfthe returned list. I can use some ugly tricks to push in function, side effects, but I try to avoid it.

How do I change the list property that I got by searching for the property as a string? Something equivalent:

(push 1 (getf-string-equal *test* "v1"))

Thank.

+4
source share
1

. , , . .

. -, push - , setf. , setf getf-string-equal. .

(defsetf getf-string-equal (plist indicator) (value)
  (let ((i (gensym))
        (rest (gensym))
        (match (gensym)))
    `(let ((,match (loop
                      for (,i . ,rest) on ,plist by #'cddr
                      when (string-equal ,i ,indicator)
                      return ,rest)))
       (if ,match
           (setf (car ,match) ,value)
           nil))))

. defsetf . plist indicator - , , value - . ; . . gensym .

, , . , cons, . , car , . , , .

, . cons , car. if. `(setf ,plist ,value), . defsetf , . , , , , define-setf-expander, defsetf. , setf, , , .

+3

Source: https://habr.com/ru/post/1692011/


All Articles