fork(1) download
  1. ; Counting the common ingredients (occurN)
  2. ; ------------------------------
  3. ; The Little Lisper 3rd Edition
  4. ; Chapter 5
  5. ; Exercise 4
  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 add1 (n)
  25. (cond
  26. ((null n) '())
  27. ((+ n 1))))
  28.  
  29. (print (add1 1))
  30.  
  31. (defun occurNa (a1 lat)
  32. (cond
  33. ((null lat) 0)
  34. ((null a1) 0)
  35. (t (cond
  36. ((eq (car lat) a1)
  37. (add1 (occurNa a1 (cdr lat))))
  38. (t (occurNa a1 (cdr lat)))))))
  39.  
  40. (print (occurNa 'c (list 'a 'b 'c)))
  41. (print (occurNa 'bananas lat2))
  42.  
  43. (defun occurN (alat lat)
  44. (cond
  45. ((null alat) 0)
  46. ((null lat) 0)
  47. (t (+ (occurNa (car alat) lat)
  48. (occurN (cdr alat) lat)))))
  49.  
  50. (print (occurN (list 'bananas) (list 'bananas 'peaches)))
  51. ; 1
  52. (print (occurN lat1 l4))
  53. ; 0
  54. (print (occurN lat1 lat2))
  55. ; 1
  56. (print (occurN lat1 lat3))
  57. ; 2
  58.  
Success #stdin #stdout 0s 10592KB
stdin
Standard input is empty
stdout
2 
1 
1 
1 
0 
1 
2