Nchen
Nchen

Reputation: 13

LISP "Error Illegal function object..."

I just started learning lisp in my class. I'm doing a homework assignment in which I have to program a few very basic functions using some primitives operations such as car, cdr, cons, append, reverse, atom, eq, equal, and null.

So here is my code so far:

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

When I try to run my test:

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

I get an "Error: Illegal function object: (car l1)." I was under the impression that I was writing the code correctly. What am I doing wrong? Thank you for your time.

Upvotes: 1

Views: 3100

Answers (2)

finnw
finnw

Reputation: 48659

Do not put extra parentheses around argument lists.

  • (member((car l1) l2)) should be (member (car l1) l2)
  • (union2((cdr l1) l2)) should be (union2 (cdr l1) l2)

Upvotes: 4

Rainer Joswig
Rainer Joswig

Reputation: 139401

Try using a compiler, like 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

Upvotes: 1

Related Questions