Recursive 'member-of'
Posted: Sat Mar 14, 2009 9:11 am
I'm trying to write a 'member-of' function, to check whether an element is a member of a list or not.
My first attempt, the iterative one, is pretty easy:
Now I want to make the recursive version.
Of course that only works if the element is indeed a member of the list.
If the element isn't, this function will not terminate.
How to fix this?
My first attempt, the iterative one, is pretty easy:
Code: Select all
(defun member-of (elem the-list)
(if (eql elem (car (member elem the-list)))
'T
'NIL)
Code: Select all
(defun member-of (elem the-list)
(if (eql elem (car the-list))
'T
(member-of elem (cdr the-list))))
If the element isn't, this function will not terminate.
How to fix this?