List 消除lisp中所有后续的数字
我是lisp新手,我在作业中遇到了一个问题,要求删除所有后面的数字,只有第一个数字会出现在列表中 例如,List 消除lisp中所有后续的数字,list,lisp,List,Lisp,我是lisp新手,我在作业中遇到了一个问题,要求删除所有后面的数字,只有第一个数字会出现在列表中 例如,(12311)将是(1231) 我的代码是: ;the input is (1 1 2 1 3 1 1 1) (defun eli(x) ; this condition will check if x is empty or has only one element (if (or(null x)(null (cdr x))) x ; if the first elem
(12311)
将是(1231)
我的代码是:
;the input is (1 1 2 1 3 1 1 1)
(defun eli(x)
; this condition will check if x is empty or has only one element
(if (or(null x)(null (cdr x))) x
; if the first element is 1 but the second element is not 1
(if (and (= 1 (car x))(not (= 1 (car (cdr x)))))
; if true then append 1 and call the function with the rest of the list
(cons (car x)(eli(cdr x)))
; if false call the function recursivaly
(eli(cdr x))
)))
; the output is (1 2 1 3 1 1)
该代码生成(1 2 1 3 1)
知道我哪里做错了吗?你的问题是这个谓词:
(and (= 1 (car x))(not (= 1 (car (cdr x))))))
这只是检查第一个元素是否为
1
,第二个元素是否为。您应该检查第一个元素是否为numberp
,然后检查前两个元素是否为eql
,然后跳过该元素 目前还不清楚产生了什么样的输入和输出。您的代码应该做什么也不清楚。您可能需要对代码进行注释。为了更清楚,我对代码进行了注释,请注意实际输出必须是(1 2 1 3 1)
。我找不到,请告诉我们Lisp在哪里为一些输入生成输出。发布实际的Lisp交互。将互动添加到你的问题中。无论是谁设置了这个问题,都可能是想让它适用于1以外的数字。。。