是否存在 setfable nthcdr 实现?

发布于 2024-10-07 02:43:36 字数 52 浏览 0 评论 0原文

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

I am using clisp and I wonder if there is any library with a setfable version of nthcdr that I can use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2024-10-14 02:43:36

你可以用以下方法解决它:

(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 定义为最后一个,而不是 nthcdr 。

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

You can hack around it with:

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

Or define your own setf for it:

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

I do not know why nthcdr does not have a setf defined. Even Alexandria seems to define setf for last, but not nthcdr.

PS. I would treat wanting to setf an nthcdr as a bad smell in your code.

森林迷了鹿 2024-10-14 02:43:36
(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 是数字时才起作用,或者在扩展代码中包含检查。

(defsetf my-nthcdr (n list) (store)
  (let ((tmp (gensym)))
    `(let ((,tmp (nthcdr (1- ,n) ,list)))
       (rplacd ,tmp ,store)
       (cdr ,tmp))))

Doesn't work when n is 0 though, you could make it work when LIST is a symbol, checking if N is zero either on macroexpansion time, but then it only works if N is a number, or including the check in the expanded code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文