7

我正在使用 clisp,我想知道是否有任何库可以使用可设置的 nthcdr 版本。

4

2 回答 2

5

您可以使用以下方法破解它:

(let ((lst (list 1 2 3 4))
      (n 2))
  (setf (cdr (nthcdr (1- n) lst)) '(5 6 7))
  l)
> (1 2 5 6 7)

或者为它定义你自己的 setf :

;; !!warning!!  only an example, lots of nasty edge cases
(defsetf nthcdr (n lst) (new-val) 
   `(setf (cdr (nthcdr (1- ,n) ,lst)) ,new-val))

我不知道为什么 nthcdr 没有定义 setf。甚至Alexandria似乎也将 setf 定义为 last,但不是 nthcdr。

PS。我会将想要设置 nthcdr 视为代码中的难闻气味。

于 2010-12-08T14:00:27.787 回答
1
(defsetf my-nthcdr (n list) (store)
  (let ((tmp (gensym)))
    `(let ((,tmp (nthcdr (1- ,n) ,list)))
       (rplacd ,tmp ,store)
       (cdr ,tmp))))

当 n 为 0 时不起作用,您可以在 LIST 是符号时使其工作,在宏扩展时检查 N 是否为零,但它仅在 N 是数字时才有效,或者在扩展代码中包含检查.

于 2010-12-08T13:53:09.480 回答