Contents    Page-10    Prev    Next    Page+10    Index   

Set as Linked List

A linked list can be used as a representation of a set.[Clojure has sets as a built-in type, which is more efficient for large sets than what is shown here.] member (written ) tests whether a given item is an element of the list. member returns the remainder of the list beginning with the desired element, although usually member is used as a predicate to test whether the element is present or not.


(member 'b '(a b c))    ->    (b c)

(member 'f '(a b c))    ->    nil


(defn member [item lst]
  (if (empty? lst)
      nil
      (if (= item (first lst))
          lst
          (member item (rest lst)) ) ) )