fork download
  1. ; Substituting two things for kiwis
  2. ; ------------------------------
  3. ; The Little Lisper 3rd Edition
  4. ; Chapter 5
  5. ; Exercise 2
  6. ; Common Lisp
  7. ; http://t...content-available-to-author-only...r.com/thelittlelisper
  8. ; http://t...content-available-to-author-only...t.com/2010/06/little-lisper-chapter-5-multichapter.html
  9. ; http://t...content-available-to-author-only...t.com/2010/06/little-lisper.html
  10. ; ------------------------------
  11. (setf x 'comma)
  12. (setf y 'dot)
  13. (setf a 'kiwis)
  14. (setf b 'plums)
  15. (setf lat1 '(bananas kiwis))
  16. (setf lat2 '(peaches apples bananas))
  17. (setf lat3 '(kiwis pears plums bananas cherries))
  18. (setf lat4 '(kiwis mangoes kiwis guavas kiwis))
  19. (setf l1 '((curry) () (chicken) ()))
  20. (setf l2 '((peaches) (and cream)))
  21. (setf l4 '())
  22. ; ------------------------------
  23.  
  24. (defun subst-sauce (a lat)
  25. (cond
  26. ((null lat) '())
  27. (t (cond
  28. ((eq (car lat) 'sauce)
  29. (cons a (cdr lat)))
  30. (t (cons (car lat)
  31. (subst-sauce
  32. a (cdr lat))))))))
  33.  
  34. (defun subst2 (new o1 o2 lat)
  35. (cond
  36. ((null lat) '())
  37. (t (cond
  38. ((eq (car lat) o1)
  39. (cons new (cdr lat)))
  40. ((eq (car lat) o2)
  41. (cons new (cdr lat)))
  42. (t (cons (car lat)
  43. (subst2 new
  44. o1 o2 (cdr lat))))))))
  45.  
  46. (setf new1 'vanilla)
  47. (setf o11 'chocolate)
  48. (setf o21 'banana)
  49. (setf mylat '(banana ice cream with chocolate topping))
  50.  
  51. (print (subst2 new1 o11 o21 mylat))
  52. ;(vanilla ice cream with chocolate topping)
  53.  
  54. (defun multisubst2 (new o1 o2 lat)
  55. (cond
  56. ((null lat) '())
  57. (t (cond
  58. ((eq (car lat) o1)
  59. (multisubst2 new o1 o2 (cons new (cdr lat))))
  60. ((eq (car lat) o2)
  61. (multisubst2 new o1 o2 (cons new (cdr lat))))
  62. (t (cons (car lat)
  63. (multisubst2 new
  64. o1 o2 (cdr lat))))))))
  65.  
  66. (print (multisubst2 x a b lat1))
  67. ;(bananas comma)
  68. (print (multisubst2 y a b lat3))
  69. ;(dot pears dot bananas gerries)
  70. (print (multisubst2 a x y lat1))
  71. ;(bananas kiwis)
  72.  
  73.  
Success #stdin #stdout 0.02s 10552KB
stdin
Standard input is empty
stdout
(VANILLA ICE CREAM WITH CHOCOLATE TOPPING) 
(BANANAS COMMA) 
(DOT PEARS DOT BANANAS CHERRIES) 
(BANANAS KIWIS)