; Use a continuation function instead of an evaluator
; ------------------------------
; The Little Lisper 3rd Edition
; Chapter 9
; 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-9-lamdba-ultimate.html
; http://t...content-available-to-author-only...t.com/2010/06/little-lisper.html
; ------------------------------
;Note multi-subst is from chapter 5 exercises - accumulators in 6
;original
(setf x 'comma)
(setf y 'dot)
(setf a 'kiwis)
(setf b 'plums)
(setf lat1 '(bananas kiwis))
(setf lat3 '(kiwis pears plums bananas cherries))
(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 cherries)
(print (multisubst2 a x y lat1))
;(bananas kiwis)
(defun multisubst (new old lat)
(cond
((null lat) '())
(t (cond
((eq (car lat) old)
(cons new
(multisubst
new old (cdr lat))))
(t (cons (car lat)
(multisubst
new old (cdr lat))))))))
(print (multisubst 'sandwich 'club '(sandwich club sandwich club)))
;(SANDWICH SANDWICH SANDWICH SANDWICH)
(defun multisubst-k (new old lat k)
(cond
((null lat) (funcall k '()))
((eq (car lat) old)
(multisubst-k new old (cdr lat)
(lambda (d)
(funcall k (cons new d)))))
(t (multisubst-k new old (cdr lat)
(lambda (d)
(funcall k (cons (car lat) d)))))))
(print (multisubst-k 'sandwich 'club '(sandwich club sandwich club) (function (lambda (x) x))))
;(SANDWICH SANDWICH SANDWICH SANDWICH)
(print (multisubst-k 'y 'x '(u v x x y z z) (function (lambda (x) x))))
;(U V Y Y Y Z Z)
;Comparison of steps
; Things you need to do when you return from a recursive function
; corresponding continuation function
; Instead of just returning a (quote()) function - you need to send the quote
; function to the continuation - and let it return it
; otherwise you call the continuation to escape the recursion ?
; so instead of consing the result of the recursion on the parent function
; you're consing the results of the continuation...