; Rewrite member for the ycombinator
; ------------------------------
; The Little Lisper 3rd Edition
; Chapter 9
; 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-9-lamdba-ultimate.html
; http://t...content-available-to-author-only...t.com/2010/06/little-lisper.html
; ------------------------------
;Y-combinator for functions of two arguments
;rewritten for lisp-2 namespace
(defun Y2 (M)
((lambda (future)
(funcall M (function (lambda (arg1 arg2)
(funcall (funcall future future) arg1 arg2)))))
(function (lambda (future)
(funcall M (lambda (arg1 arg2)
(funcall (funcall future future) arg1 arg2)))))))
;assuming reference to member refers to chapter 2 (for member function) and not 3 (typo)
(setf (symbol-function 'member-Y)
(Y2 (function (lambda (recurring-function)
(function (lambda (a lat)
(cond
((null lat) nil)
(t (or
(eq (car lat) a)
(funcall recurring-function a (cdr lat)))))))))))
;l)))
(print (member-Y 'a '(a b c d e f g)))
;T
(print (member-Y 'b '(a b c d e f g)))
;T
(print (member-Y 'c '(a b c d e f g)))
;T
(print (member-Y 'z '(a b c d e f g)))
;NIL false
;but rember does come from chapter 3 (not a typo)
(setf (symbol-function 'rember-Y)
(Y2 (function (lambda (recurring-function)
(function (lambda (a lat)
(cond
((null lat) '())
(t (cond
((eq (car lat) a) (cdr lat))
(t (cons (car lat)
(funcall recurring-function a (cdr lat)))))))))))))
(print (rember-Y 'and '(bacon lettuce and tomato)))
;(BACON LETTUCE TOMATO)
;insertR
(defun YN (F)
((lambda (future)
(funcall F (function(lambda (&rest args)
(apply (funcall future future) args)))))
#'(lambda (future)
(funcall F (function(lambda (&rest args)
(apply (funcall future future) args))))) ) )
(setf (symbol-function 'insertR-Y)
(YN (function (lambda (recurring-function)
(function (lambda (new old lat)
(cond
((null lat) '())
(t (cond
((eq (car lat) old)
(cons old
(cons new (cdr lat))))
(t (cons (car lat)
(funcall recurring-function
new old (cdr lat)))))))))))))
(print (insertR-Y 'toasted 'club '(large club sandwich)))
;(LARGE CLUB TOASTED SANDWICH)
;subst-2
(setf (symbol-function 'subst2-Y)
(YN (function (lambda (recurring-function)
(function (lambda (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)
(funcall recurring-function new
o1 o2 (cdr lat)))))))))))))
(print (subst2-Y 'vanilla 'chocolate 'banana '(banana icecream with chocolate topping)))
;(VANILLA ICECREAM WITH CHOCOLATE TOPPING)
(print (subst2-Y 'vanilla 'chocolate 'topping '(banana icecream with chocolate topping)))
;(BANANA ICECREAM WITH VANILLA TOPPING)