是否存在可设置的nthcdr实现? [英] Does a setfable nthcdr implementation exist?

查看:69
本文介绍了是否存在可设置的nthcdr实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

(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.甚至亚历山大似乎都为最后一个定义了setf,但没有为nthcdr定义. >

PS.我希望将nthcdr设置为您的代码中的难闻气味.

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

解决方案

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.

这篇关于是否存在可设置的nthcdr实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆