Search code examples
lispcommon-lispmembership

check element if a member of a list


i want to do a iterative function to compute if an element el is a member of a list and returns an atom.

;;; this is the list (setf lst '(a b c d e 1 2 3 4 5))

;;;this is the function to check whether it is a member of the list.

(defun checklist (a lst)
           (if (member a alist)
               a))

when i put the input -> (checklist 1 lst) , it return the number ... but when i input alphabets, it return me error. .. anyone can help me state out what is the problem ?? thanks in advance .

================================================================================

when i input --> (question5 a lst)

it give me error like this .

CG-USER(7): (question5 a lst) Error: Attempt to take the value of the unbound variable `A'. [condition type: UNBOUND-VARIABLE]


Solution

  • Is that homework?

    If yes:

    I don't think member is useful for you. If you want to do an iterative solution, then you would use something like DO or DOLIST.

    If no:

    Common Lisp has a function which finds elements in sequence (list, vector, string, ...): FIND.

    Added: use (question5 'a lst)

    Also note that your definition has an unfortunate part:

    (checklist nil '(a b c))
    
    (checklist nil '(a b c nil))
    

    both would return NIL.

    Btw., the Lisp introduction book from Touretzky is free for download: http://www.cs.cmu.edu/~dst/LispBook/ The book provides a nice introduction into the basics of Lisp programming.