0

I 'am a beginner in LISP.I use clisp in ubuntu.I have a code here in lisp to carry out union operation on two lists.The logic is correct.But stuck at an error which is:

*** - APPEND: A proper list must not end with T

my code is:

(defun set-union (L1 L2)
(cond 
 ((null L2)   ;if l2 is null then union is l1 itself. 
  L1) 
 ((not (member (first L2) L1))  ;check if first member of l2 is in l1 or not
  (setq l1 (append (set-union L1 (rest L2)) (list (first L2))))) ;if not then append it with the union of l1 and rest of l2.
 (t
  (set-union L1 (rest L2)))
  )) ;if second condition is not true then carry out union on l1 and rest of the elements of l2
(setq l1 (list 'a 'c 'b 'g))
(setq l2 (list 'd 'g 't))
(set-union l1 l2)
(print l1)

I Need help!!Thanks.

Midhun
  • 744
  • 2
  • 15
  • 31

1 Answers1

1
(append (set-union L1 (rest L2)) (first L2))

at some point of your logic tries to append (A C B G . T) and E, which fails because the first is not a proper list.

Either use

(append (set-union L1 (rest L2)) (list (first L2)))

or, better

(cons (first L2) (set-union L1 (rest L2)))
uselpa
  • 18,732
  • 2
  • 34
  • 52
  • Sir i have tried this.This time no errors but the output printed is the first list and not the union of lists.Thanks @uselpa – Midhun Oct 31 '15 at 14:12
  • With your updated code `(set-union l1 l2)` produces `(A C B G T D)` which is correct. But `set-union` does not alter l1 or l2, it just returns the result. After that you print l1 which has not changed. – uselpa Oct 31 '15 at 14:18
  • So what you probably want is `(setq l1 (set-union l1 l2))`, after which l1 has been updated and `(print l1)` produces the result you expect. – uselpa Oct 31 '15 at 14:24
  • Yes sir that's exactly what i want – Midhun Oct 31 '15 at 15:44