2016-10-01 78 views
0

我想製作一個程序,它將重寫給定的行而不在輸出行中有重複項。我使用這個網站,作爲一個編譯器: https://www.tutorialspoint.com/execute_lisp_online.phpCommon Lisp lambda表達式錯誤

這裏是我的代碼

(SETQ X (LIST 2 -3 (LIST 4 3 0 2) (LIST 4 -4) (LIST 2 (LIST 2 0 2))-3))' 
(DEFUN SEARCHDEEP (WHAT WHERE) ;Function will find out if atom `WHAT`is in a row `WHERE` => works fine 
(COND 
    ((NULL WHERE) NIL) 
    (T (OR 
      (COND 
       ((ATOM (CAR WHERE)) (EQUAL WHAT (CAR WHERE))) 
       (T (SEARCHDEEP WHAT (CAR WHERE))) 
      ) 
      (SEARCHDEEP WHAT (CDR WHERE)) 
     ) 
    ) 
) 
) 

(DEFUN REMDOUBLES (INPUT OUTPUT) 
(
(COND 
    ((NULL INPUT) NILL) ;recursion exit 
    (T     ; funstion 
     (OR    ; step into or go forward 
      (COND 
       ((ATOM (CAR INPUT)) (COND 
             ((NOT (SEARCHDEEP (CAR INPUT) OUTPUT)) (APPEND OUTPUT INPUT)) ;if this atom wasn't added => append 
            ) 
       ) 
       (T (REMDOUBLES (CAR INPUT) OUTPUT)) ; step into (car input => list 
      ) 
      (REMDOUBLES (CRD INPUT) OUTPUT) ; go forward, car input is anatom 
     ) 
    ) 
) 
) 
) 

(SETQ OUT (QUOTE)) ;Empty row 
(REMDOUBLES X OUT) 
(PRINT OUT) 

我花了2小時以來檢查這些代碼,並在這裏其他的答案,堆棧,但我不知道是什麼我在這裏想念嗎?

我收到此錯誤:

*** - SYSTEM::%EXPAND-FORM:
(COND ((NULL INPUT) NILL)(T(OR (COND ((ATOM (CAR INPUT)) (COND ((NOT (SEARCHDEEP (CAR INPUT) OUTPUT))(APPEND OUTPUT INPUT)))) (T (REMDOUBLES (CAR INPUT) OUTPUT))) (REMDOUBLES (CRD INPUT) OUTPUT)))) should be a lambda expression

很抱歉的格式,我在函數式編程和Lisp新手,我不知道應該如何正確地完成。

+2

'(DEFUN REMDOUBLES(輸入輸出)'和'(COND')之間有一個額外的左括號。正確縮進代碼!任何半像素的編輯都會爲你縮進。 – Gilles

+1

在第('(NULL INPUT)NILL)行中還有一個錯誤,應該是'((NULL INPUT)NIL)' –

回答

5

您的代碼:

第一行:

(DEFUN REMDOUBLES (INPUT OUTPUT) 

下聯:你能解釋一下單括號應該做些什麼?

(

你記住基本的Lisp表達式的語法Lisp的?

(operator argument0 argument1 ... argumentn) 

這不是

((operator argument0 argument1 ... argumentn)) 

Sorry for the formatting, i am novice in functional programming and LISP, and i have no idea how it should be done properly.

下載從Touretzky介紹Lisp的書:https://www.cs.cmu.edu/~dst/LispBook/

然後瞭解有代碼的Lisp的樣子。

您還可以使用Lisp的格式化代碼爲您提供:

這是您的格式化代碼:

[4]> (pprint '(DEFUN SEARCHDEEP (WHAT WHERE) ;Function will find out if atom `WHAT`is in a row `WHERE` => works fine 
(COND 
    ((NULL WHERE) NIL) 
    (T (OR 
      (COND 
       ((ATOM (CAR WHERE)) (EQUAL WHAT (CAR WHERE))) 
       (T (SEARCHDEEP WHAT (CAR WHERE))) 
      ) 
      (SEARCHDEEP WHAT (CDR WHERE)) 
     ) 
    ) 
) 
)) 

這是格式化代碼:

(DEFUN SEARCHDEEP (WHAT WHERE) 
(COND ((NULL WHERE) NIL) 
    (T 
    (OR 
    (COND ((ATOM (CAR WHERE)) (EQUAL WHAT (CAR WHERE))) 
    (T (SEARCHDEEP WHAT (CAR WHERE)))) 
    (SEARCHDEEP WHAT (CDR WHERE)))))) 

但它會更好寫成:

(DEFUN SEARCHDEEP (WHAT WHERE) 
    (WHEN WHERE 
    (OR (IF (ATOM (CAR WHERE)) 
      (EQUAL WHAT (CAR WHERE)) 
      (SEARCHDEEP WHAT (CAR WHERE))) 
     (SEARCHDEEP WHAT (CDR WHERE)))))