LISP“错误非法函数对象......”

时间:2012-11-03 21:20:31

标签: common-lisp

我刚开始在课堂上学习lisp。我正在做一个家庭作业,我必须使用一些基本操作来编写一些非常基本的函数,例如car,cdr,cons,append,reverse,atom,eq,equal和null。

所以这是我目前的代码:

(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)。”我的印象是我正确地编写了代码。我究竟做错了什么? 谢谢你的时间。

2 个答案:

答案 0 :(得分:3)

不要在参数列表周围添加额外的括号。

  • (member((car l1) l2))应为(member (car l1) l2)
  • (union2((cdr l1) l2))应为(union2 (cdr l1) l2)

答案 1 :(得分: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