; Counting the common ingredients (occurN)
; ------------------------------
; The Little Lisper 3rd Edition
; Chapter 5
; Exercise 4
; 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-5-multichapter.html
; http://t...content-available-to-author-only...t.com/2010/06/little-lisper.html
; ------------------------------
(setf x 'comma)
(setf y 'dot)
(setf a 'kiwis)
(setf b 'plums)
(setf lat1 '(bananas kiwis))
(setf lat2 '(peaches apples bananas))
(setf lat3 '(kiwis pears plums bananas cherries))
(setf lat4 '(kiwis mangoes kiwis guavas kiwis))
(setf l1 '((curry) () (chicken) ()))
(setf l2 '((peaches) (and cream)))
(setf l4 '())
; ------------------------------
(defun add1 (n)
(cond
((null n) '())
((+ n 1))))
(print (add1 1))
(defun occurNa (a1 lat)
(cond
((null lat) 0)
((null a1) 0)
(t (cond
((eq (car lat) a1)
(add1 (occurNa a1 (cdr lat))))
(t (occurNa a1 (cdr lat)))))))
(print (occurNa 'c (list 'a 'b 'c)))
(print (occurNa 'bananas lat2))
(defun occurN (alat lat)
(cond
((null alat) 0)
((null lat) 0)
(t (+ (occurNa (car alat) lat)
(occurN (cdr alat) lat)))))
(print (occurN (list 'bananas) (list 'bananas 'peaches)))
; 1
(print (occurN lat1 l4))
; 0
(print (occurN lat1 lat2))
; 1
(print (occurN lat1 lat3))
; 2