; Fish and chips - finding the first nested occurrence
; ------------------------------
; The Little Lisper 3rd Edition
; Chapter 6
; Exercise 5
; Common Lisp
; http://t...content-available-to-author-only...r.com/thelittlelisper
; http://t...content-available-to-author-only...t.com/2010/06/little-lisper-chapter-6-oh-my-gawd-its.html
; http://t...content-available-to-author-only...t.com/2010/06/little-lisper.html
; ------------------------------
(setf l1 '((fried potatoes)(baked (fried)) tomatoes))
(setf l2 '(((chili) chili (chili))))
(setf l3 '())
(setf lat1 '(chili and hot)) 
(setf lat2 '(baked fried)) 
(setf a 'fried)
; ------------------------------

(defun member* (a l)
  (cond
   ((null l) NIL)
   ((atom (car l))
    (or
     (eq (car l) a)
     (member* a (cdr l))))
   (t (or 
       (member* a (car l))
       (member* a (cdr l))))))

(print (member* 'chips '((potato) (chips ((with) fish) (chips)))))
;T

(defun notatom (lat)
  (not (atom lat)))

;version that starts at the end of the list
(defun member-backwards* (a l oldl)
  (cond
   ((null l) (member* a oldl))
   ((notatom (car l))
             (or (member-backwards* a (cdr l) oldl)
                   (member-backwards* a (car l) oldl)))
   (t 
    (member-backwards* a 
                       (cdr l) 
                       (cons (car l) oldl)))))

(print (member-backwards* 'chips '((potato) (chips ((with) fish) (chips))) '()))
;T
                                     
