2017-04-24 44 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,然後跳過該元素。