【发布时间】:2017-04-24 19:42:47
【问题描述】:
我是 lisp 新手,我在作业中遇到了一个问题,要求消除所有后续数字,只有第一个数字会出现在列表中
例如(1 1 2 1 3 1 1 1) 将是(1 2 1 3 1)
我的代码是:
;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 1)
知道我哪里做错了吗?
【问题讨论】:
-
不清楚哪个输入生成哪个输出。还不清楚您的代码应该做什么。您可能想评论您的代码。
-
我注释我的代码更清晰,注意真正的输出必须是
(1 2 1 3 1)。我无法得到它 -
请告诉我们 Lisp 在哪里为某些输入生成输出。发布实际的 Lisp 交互。将互动添加到您的问题中。
-
设置问题的人可能希望它适用于 1 以外的数字...