fork download
  1. ; Finding the common ingredients
  2. ; ------------------------------
  3. ; The Little Lisper 3rd Edition
  4. ; Chapter 5
  5. ; Exercise 5
  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 I_ (a_ lat2_)
  25. (cond
  26. ((null a_) '())
  27. ((null lat2_) '())
  28. (t (cond
  29. ((eq (car lat2_) a_)
  30. a_)
  31. (t (I_ a_ (cdr lat2_)))))))
  32.  
  33. (print (I_ 'bananas (list 'bananas 'peaches)))
  34.  
  35. (defun I_exists (a_ lat2_)
  36. (cond
  37. ((null a_) '())
  38. ((null lat2_) '())
  39. (t (cond
  40. ((eq (car lat2_) a_)
  41. t)
  42. (t (I_ a_ (cdr lat2_)))))))
  43.  
  44. (print (I_exists 'bananas (list 'bananas 'peaches)))
  45.  
  46. (defun I (lat1_ lat2_)
  47. (cond
  48. ((null lat1_) '())
  49. ((null lat2_) '())
  50. (t (cond
  51. ((I_exists (car lat2_) lat1_)
  52. (I_ (car lat2_) lat1_))
  53. (t (I (cdr lat2_) lat1_))))))
  54.  
  55. (print (I (list 'apples 'bananas) (list 'bananas 'peaches)))
  56. (print (I (list 'apples 'bananas 'pears) (list 'bananas 'peaches 'pears)))
  57.  
  58. (defun multiI (lat1_ lat2_)
  59. (cond
  60. ((null lat1_) '())
  61. ((null lat2_) '())
  62. (t (cond
  63. ((I_exists (car lat2_) lat1_)
  64. (cons (car lat2_) (multiI (cdr lat2_) lat1_)))
  65. (t (multiI (cdr lat2_) lat1_))))))
  66.  
  67. (print (multiI (list 'apples 'bananas 'pears) (list 'bananas 'peaches 'pears)))
  68.  
  69. (print (I lat1 l4))
  70. ;()
  71. (print (I lat1 lat2))
  72. ;bananas
  73. (print (I lat1 lat3))
  74. ;kiwis
  75. (print (multiI lat1 l4))
  76. ;()
  77. (print (multiI lat1 lat2))
  78. ;(bananas)
  79. (print (multiI lat1 lat3))
  80. ;(kiwis bananas)
  81.  
  82.  
Success #stdin #stdout 0s 10600KB
stdin
Standard input is empty
stdout
BANANAS 
T 
BANANAS 
BANANAS 
(BANANAS PEARS) 
NIL 
BANANAS 
KIWIS 
NIL 
(BANANAS) 
(KIWIS BANANAS)