Member using MAP
Posted: Mon Dec 19, 2011 2:34 pm
I must write a function that checks whether an atom is a member of a list not necessarily linear.
This is my piece of code, but it works only for linear lists.
Can anyone see where I'm wrong?
example:
(memberl '1 '(1 2 3 4)) ->T
(memberl '1 (2 3 4)) ->NIL
(memberl '1 '(2 4 5 6 (1 9))) ->NIL (expected: T)
Thanks a lot.
This is my piece of code, but it works only for linear lists.
Can anyone see where I'm wrong?
Code: Select all
(defun myor(l)
(cond
((null l) l)
((atom l) T)
((not (null (car l))) T)
(T (myor (cdr l)) )
)
)
(defun memberl(e l)
(cond
((and (atom l) (equal e l)) (list T) )
((atom l) (list NIL))
(T (myor (mapcan #'(lambda (l) (memberl e l) ) l ) ))
)
)
(memberl '1 '(1 2 3 4)) ->T
(memberl '1 (2 3 4)) ->NIL
(memberl '1 '(2 4 5 6 (1 9))) ->NIL (expected: T)
Thanks a lot.