List 使用Lisp将新元素推送到新的空列表中
下面的函数尝试使用元素功能更新List 使用Lisp将新元素推送到新的空列表中,list,common-lisp,element,List,Common Lisp,Element,下面的函数尝试使用元素功能更新*功能列表*。 要素列表定义为全局变量 当我针对空的*功能列表*运行函数时,会收到一条错误消息 The object is a CONDITION of type TYPE-ERROR. DATUM: NIL EXPECTED-TYPE: CONS 但是,当我初始化*功能列表*时,函数会正确执行 (defun update-feature-list (feature) (let ((feature-index)) (setq feature-index
*功能列表*
。
要素列表定义为全局变量
当我针对空的*功能列表*
运行函数时,会收到一条错误消息
The object is a CONDITION of type TYPE-ERROR.
DATUM: NIL
EXPECTED-TYPE: CONS
但是,当我初始化*功能列表*
时,函数会正确执行
(defun update-feature-list (feature)
(let ((feature-index))
(setq feature-index (position feature *features-list*))
(cond ((equal feature-index nil)
(push feature (cdr (last *features-list*)))
(setq feature-index (position feature *features-list*))))))
使用标准格式。看 带有一个子句的
cond
是没有意义的。我猜你的意思是当时,你
在任何情况下,我都想更新索引。E在条件之外
(defun update-feature-list (feature)
(let ((feature-index))
(setq feature-index (position feature *features-list*))
(when (equal feature-index nil)
(push feature (cdr (last *features-list*))))
(setq feature-index (position feature *features-list*))))
您不需要设置局部变量来返回它:
(defun update-feature-list (feature)
(let ((feature-index))
(setq feature-index (position feature *features-list*))
(when (equal feature-index nil)
(push feature (cdr (last *features-list*))))
(position feature *features-list*)))
您可以直接在let
标题中创建绑定:
(defun update-feature-list (feature)
(let ((feature-index (position feature *features-list*)))
(when (equal feature-index nil)
(push feature (cdr (last *features-list*))))
(position feature *features-list*)))
使用null
,而不是检查equal
…nil
:
(defun update-feature-list (feature)
(let ((feature-index (position feature *features-list*)))
(when (null feature-index)
(push feature (cdr (last *features-list*))))
(position feature *features-list*)))
您可以内联该变量:
(defun update-feature-list (feature)
(when (null (position feature *features-list*))
(push feature (cdr (last *features-list*))))
(position feature *features-list*))
不要使用null
…位置
,而是使用非
…成员
:
(defun update-feature-list (feature)
(when (not (member feature *features-list*))
(push feature (cdr (last *features-list*))))
(position feature *features-list*))
列表中最后一个cons的cdr
不是您想要推送的位置
事情进展顺利。我猜您希望追加,但在大多数情况下,您应该这样做
相反,推到列表的最前面,这样更有效。
对于这一点,还有pushnew
。返回新职位并不意味着
然而,现在有很多道理:
(defun update-feature-list (feature)
(pushnew feature *features-list*)
(position feature *features-list*))
如果确实需要此顺序和位置,请使用可调整向量:
(defvar *features-list* (make-array 10
:adjustable t
:fill-pointer 0))
(defun add-feature (feature)
(or (position feature *features-list*)
(vector-push-extend feature *features-list*))) ; v-p-e returns the index
使用标准格式。看
带有一个子句的cond
是没有意义的。我猜你的意思是当
时,你
在任何情况下,我都想更新索引。E在条件之外
(defun update-feature-list (feature)
(let ((feature-index))
(setq feature-index (position feature *features-list*))
(when (equal feature-index nil)
(push feature (cdr (last *features-list*))))
(setq feature-index (position feature *features-list*))))
您不需要设置局部变量来返回它:
(defun update-feature-list (feature)
(let ((feature-index))
(setq feature-index (position feature *features-list*))
(when (equal feature-index nil)
(push feature (cdr (last *features-list*))))
(position feature *features-list*)))
您可以直接在let
标题中创建绑定:
(defun update-feature-list (feature)
(let ((feature-index (position feature *features-list*)))
(when (equal feature-index nil)
(push feature (cdr (last *features-list*))))
(position feature *features-list*)))
使用null
,而不是检查equal
…nil
:
(defun update-feature-list (feature)
(let ((feature-index (position feature *features-list*)))
(when (null feature-index)
(push feature (cdr (last *features-list*))))
(position feature *features-list*)))
您可以内联该变量:
(defun update-feature-list (feature)
(when (null (position feature *features-list*))
(push feature (cdr (last *features-list*))))
(position feature *features-list*))
不要使用null
…位置
,而是使用非
…成员
:
(defun update-feature-list (feature)
(when (not (member feature *features-list*))
(push feature (cdr (last *features-list*))))
(position feature *features-list*))
列表中最后一个cons的cdr
不是您想要推送的位置
事情进展顺利。我猜您希望追加,但在大多数情况下,您应该这样做
相反,推到列表的最前面,这样更有效。
对于这一点,还有pushnew
。返回新职位并不意味着
然而,现在有很多道理:
(defun update-feature-list (feature)
(pushnew feature *features-list*)
(position feature *features-list*))
如果确实需要此顺序和位置,请使用可调整向量:
(defvar *features-list* (make-array 10
:adjustable t
:fill-pointer 0))
(defun add-feature (feature)
(or (position feature *features-list*)
(vector-push-extend feature *features-list*))) ; v-p-e returns the index
Joshua Taylor提供的解决方案
您的“将某个val推送到函数内的列表”方法
不起作用;如果原始列表为空,会发生什么情况?你不能
修改空列表的car或cdr;它没有这些。那个
如果你真的想使用你描述的方法,你可以这样做
使用(push newvalue(rest list))
(rotatef(第一个列表)(第二个列表))
可以更清楚地看到它。(这当然不是唯一的选择,
不过。)
Joshua Taylor提供的解决方案
您的“将某个val推送到函数内的列表”方法
不起作用;如果原始列表为空,会发生什么情况?你不能
修改空列表的car或cdr;它没有这些。那个
如果你真的想使用你描述的方法,你可以这样做
使用(push newvalue(rest list))
(rotatef(第一个列表)(第二个列表))
可以更清楚地看到它。(这当然不是唯一的选择,
不过。)
请修复代码缩进和括号。现在它是不可读的。是的..知道是什么导致了错误吗?请参阅。这解释了错误并提供了解决方案!谢谢,请修复代码缩进和括号。现在它是不可读的。是的..知道是什么导致了错误吗?请参阅。这解释了错误并提供了解决方案!谢谢谢谢…但是,我确实需要元素的精确顺序..因此我无法推入列表的开头..我尝试了上面的代码更正..使用push和append..两者都有相同的行为..当列表为空时出错,当至少使用一个元素初始化时,nd正确的行为..因此,当我使用append而不是push..并将列表初始化为(defparameter features_list nil)而不是我以前初始化为(defparameter features_list')()时,它工作正常。。使用推送时,只要列表是空的,错误就会持续,与初始化方式无关。。对我来说,我现在很高兴使用append来实现这一点。但是如果有人能解释为什么push在空列表上以这种方式运行,那就太好了great@ijuio:(push'foo*list*)
与(setf*list*(cons'foo*list*))
相同<代码>(列表'a'b'c)
与(cons'a(cons'b(cons'c nil)))相同。
。这就是Lisp中列表的工作方式:它们是一个cons链。当您的变量绑定到列表时,它实际上引用“第一个”或“最外层”单元格Last
返回最后一个或“最里面”的cons单元格Nil
不是cons而是atom,它也用作空列表。谢谢…但是,我确实需要元素的精确顺序..因此我无法在列表的开头推入..我尝试了上面的代码更正..使用push和append..并且两者都有相同的行为..当列表为空时出错,当至少使用一个元素初始化时,nd正确的行为..因此,当我使用append而不是push..并将列表初始化为(defparameter features_list nil)而不是我以前初始化为(defparameter features_list')()时,它工作正常。。使用推送时,只要列表是空的,错误就会持续,与初始化方式无关。。对我来说,我现在很高兴使用append来实现这一点。但是如果有人能解释为什么push在空列表上以这种方式运行,那就太好了great@ijuio:(推送'foo*列表*)
是sa