MMM
MMM

Reputation: 19

CLISP Recursive function returning at ATOM if it is a MEMBER of a list

I need help figuring out what I have wrong with my code, this is the problem assigned:

write a recursive function named MYMEMBER that takes an ATOM (atm) and a LIST (lst) and returns the ATOM if it is a MEMBER, otherwise NIL

ex. (MYMEMBER ‘D ‘(A B C D E F G) returns => D

ex. (MYMEMBER ‘H ‘(A B C D E F G) returns => NIL

This is what I have:

(defun mymember (atm lst)
  (cond ((null lst) nil)
   ((equal atm (car lst)) lst)
   (t (mymember atm (cdr lst)))))

Mine returns:

(mymember 'd '(a b c d e f g))
; ==> (D E F G)

How do I fix it so it only returns the D, instead of return the rest of the list?

Upvotes: 1

Views: 191

Answers (1)

Sylwester
Sylwester

Reputation: 48745

Look at the first term in the cond where you compare the atom to the first element of the list. You return lst if it matches. Do you think lst, that you already have taken car of, is an atom?

Upvotes: 2

Related Questions