; Is there chili in the hot potatoes? (nested intersection)
; ------------------------------
; The Little Lisper 3rd Edition
; Chapter 6
; Exercise 2
; 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 add1 (n)
  (cond
   ((null n) '())
   ((+ n 1))))

(print (add1 1))

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

(defun occurNa* (a1 lat)
  (cond
   ((null lat) 0)
   ((null a1) 0)
   (t (cond
       ((notatom (car lat))
        (+ (occurNa* a1 (car lat))
           (occurNa* a1 (cdr lat))))
       ((eq (car lat) a1)
        (add1 (occurNa* a1 (cdr lat))))
       (t (occurNa* a1 (cdr lat)))))))

(print (occurNa* 'c (list 'a 'b 'c)))
(print (occurNa* 'bananas '(((bananas))(bananas))))

(defun occurN* (alat lat)
  (cond 
   ((null alat) 0)
   ((null lat) 0)
   ((notatom (car alat))
    (+ (occurN* (car alat) lat)
       (occurN* (cdr alat) lat)))
   (t (+ (occurNa* (car alat) lat)
         (occurN* (cdr alat)  lat)))))

(print (occurN* '(baked fried) '((fried potatoes)(baked (fried)) tomatoes)))
; 3

(print (occurN* lat1 l2))
;3

(print (occurN* lat2 l1))
;3

(print (occurN* lat1 l3))
;0
