; Substituting two things for kiwis
; ------------------------------
; The Little Lisper 3rd Edition
; Chapter 5
; 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-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 subst-sauce (a lat)
(cond
((null lat) '())
(t (cond
((eq (car lat) 'sauce)
(cons a (cdr lat)))
(t (cons (car lat)
(subst-sauce
a (cdr lat))))))))
(defun subst2 (new o1 o2 lat)
(cond
((null lat) '())
(t (cond
((eq (car lat) o1)
(cons new (cdr lat)))
((eq (car lat) o2)
(cons new (cdr lat)))
(t (cons (car lat)
(subst2 new
o1 o2 (cdr lat))))))))
(setf new1 'vanilla)
(setf o11 'chocolate)
(setf o21 'banana)
(setf mylat '(banana ice cream with chocolate topping))
(print (subst2 new1 o11 o21 mylat))
;(vanilla ice cream with chocolate topping)
(defun multisubst2 (new o1 o2 lat)
(cond
((null lat) '())
(t (cond
((eq (car lat) o1)
(multisubst2 new o1 o2 (cons new (cdr lat))))
((eq (car lat) o2)
(multisubst2 new o1 o2 (cons new (cdr lat))))
(t (cons (car lat)
(multisubst2 new
o1 o2 (cdr lat))))))))
(print (multisubst2 x a b lat1))
;(bananas comma)
(print (multisubst2 y a b lat3))
;(dot pears dot bananas gerries)
(print (multisubst2 a x y lat1))
;(bananas kiwis)