2017-04-24 16 views
-2

我是新来口齿不清,我遇到了我的家庭作业,要求消除所有的后续数字只有第一个号码将在列表中的问题 例如(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)

任何想法,我做错了什么?

+0

目前还不清楚输入哪个输出。目前还不清楚你的代码应该做什么。你可能想要评论你的代码。 –

+0

我说我的代码更清晰,请注意,实际输出必须是'(1 2 1 3 1)'。我无法得到它 –

+0

请告诉我们Lisp在哪里为某些输入生成输出。发布实际的Lisp交互。将交互添加到您的问题。 –

回答

0

你的问题是这样的断言:

(and (= 1 (car x))(not (= 1 (car (cdr x)))))) 

这只是检查,如果第一个元素是1,第二个是不是。您应该检查第一个元素是否为numberp,然后如果两个第一个元素是eql,然后跳过该元素。