cl-loop 破坏性地修改 cons 单元格

cl-loop destructively modify cons cell

我想更新 cl-loop 结构中 cons 单元格的内容。例如,假设我有以下

(setq lst '(("a" . "b") ("c" . "d")))
(cl-loop for (k . v) in lst
   when (string= k "b")
   do (setq v "f") ; how to destructively change this?
   and return t)

我在想我需要使用 setcdr,但认为这需要使用不解构的循环。我怀疑我现在拥有它的方式是否有可能没有得到指向该对象的指针,但我不确定。

一个循环中可以有多个 for 子句:

(let ((alist (copy-tree '(("a" . "b") ("b" . "c") ("c" . "d") ("b" . "e")))))
  (cl-loop for cell in alist
           for (k . v) = cell
           when (string= k "b")
           do (setcdr cell "f")
           and return t)
  alist)
;=> (("a" . "b") ("b" . "f") ("c" . "d") ("b" . "e"))