2012-11-03 136 views
1

我剛開始在我的課堂上學習lisp。我正在做一個家庭作業,其中我必須使用一些基本操作(如car,cdr,cons,append,reverse,atom,eq,equal和null)編寫幾個非常基本的函數。LISP「Error Illegal function object ...」

因此,這裏是到目前爲止我的代碼:

(defun union2 (l1 l2) 
    (cond ((null l1) l2) 
     ((member((car l1) l2)) (union2((cdr l1) l2))) 
     (t (cons (car l1) (union2((cdr l1) l2))))) 
) 

當我嘗試運行我的測試:

(union2 '(5 7 2 3 1) '(3 2 4 6 9)) 

我得到一個 「錯誤:非法函數對象:(汽車L1)。」我的印象是我正在編寫代碼。我究竟做錯了什麼? 謝謝你的時間。

回答

1

嘗試使用編譯器,像SBCL:

* (defun union2 (l1 l2) 
    (cond ((null l1) l2) 
     ((member((car l1) l2)) (union2((cdr l1) l2))) 
     (t (cons (car l1) (union2((cdr l1) l2))))) 
) 
; in: DEFUN UNION2 
;  ((CAR L1) L2) 
; 
; caught ERROR: 
; illegal function call 

;  (MEMBER ((CAR L1) L2)) 
; 
; caught WARNING: 
; The function was called with one argument, but wants at least two. 

;  ((CDR L1) L2) 
; 
; caught ERROR: 
; illegal function call 

;  (UNION2 ((CDR L1) L2)) 
; 
; caught WARNING: 
; The function was called with one argument, but wants exactly two. 

;  ((CDR L1) L2) 
; 
; caught ERROR: 
; illegal function call 

;  (UNION2 ((CDR L1) L2)) 
; 
; caught WARNING: 
; The function was called with one argument, but wants exactly two. 
; 
; compilation unit finished 
; caught 3 ERROR conditions 
; caught 3 WARNING conditions 
+0

這不是真的很有幫助。如果學生遇到一些特定的實施情況,該怎麼辦? – acelent

3

不要把周圍的參數列表額外的括號。

  • (member((car l1) l2))應該(member (car l1) l2)
  • (union2((cdr l1) l2))應該(union2 (cdr l1) l2)
相關問題