1

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.

2 Answers 2

4

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

That's not really helpful. What if the student is stuck with some specific implementation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.