; Finding the common ingredients
; ------------------------------
; The Little Lisper 3rd Edition
; Chapter 5
; 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-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 I_ (a_ lat2_)
(cond
((null a_) '())
((null lat2_) '())
(t (cond
((eq (car lat2_) a_)
a_)
(t (I_ a_ (cdr lat2_)))))))
(print (I_ 'bananas (list 'bananas 'peaches)))
(defun I_exists (a_ lat2_)
(cond
((null a_) '())
((null lat2_) '())
(t (cond
((eq (car lat2_) a_)
t)
(t (I_ a_ (cdr lat2_)))))))
(print (I_exists 'bananas (list 'bananas 'peaches)))
(defun I (lat1_ lat2_)
(cond
((null lat1_) '())
((null lat2_) '())
(t (cond
((I_exists (car lat2_) lat1_)
(I_ (car lat2_) lat1_))
(t (I (cdr lat2_) lat1_))))))
(print (I (list 'apples 'bananas) (list 'bananas 'peaches)))
(print (I (list 'apples 'bananas 'pears) (list 'bananas 'peaches 'pears)))
(defun multiI (lat1_ lat2_)
(cond
((null lat1_) '())
((null lat2_) '())
(t (cond
((I_exists (car lat2_) lat1_)
(cons (car lat2_) (multiI (cdr lat2_) lat1_)))
(t (multiI (cdr lat2_) lat1_))))))
(print (multiI (list 'apples 'bananas 'pears) (list 'bananas 'peaches 'pears)))
(print (I lat1 l4))
;()
(print (I lat1 lat2))
;bananas
(print (I lat1 lat3))
;kiwis
(print (multiI lat1 l4))
;()
(print (multiI lat1 lat2))
;(bananas)
(print (multiI lat1 lat3))
;(kiwis bananas)