fork download
  1. ; Is there chili in the hot potatoes? (nested intersection)
  2. ; ------------------------------
  3. ; The Little Lisper 3rd Edition
  4. ; Chapter 6
  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-6-oh-my-gawd-its.html
  9. ; http://t...content-available-to-author-only...t.com/2010/06/little-lisper.html
  10. ; ------------------------------
  11. (setf l1 '((fried potatoes)(baked (fried)) tomatoes))
  12. (setf l2 '(((chili) chili (chili))))
  13. (setf l3 '())
  14. (setf lat1 '(chili and hot))
  15. (setf lat2 '(baked fried))
  16. (setf a 'fried)
  17. ; ------------------------------
  18. (defun add1 (n)
  19. (cond
  20. ((null n) '())
  21. ((+ n 1))))
  22.  
  23. (print (add1 1))
  24.  
  25. (defun notatom (lat)
  26. (not (atom lat)))
  27.  
  28. (defun occurNa* (a1 lat)
  29. (cond
  30. ((null lat) 0)
  31. ((null a1) 0)
  32. (t (cond
  33. ((notatom (car lat))
  34. (+ (occurNa* a1 (car lat))
  35. (occurNa* a1 (cdr lat))))
  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 '(((bananas))(bananas))))
  42.  
  43. (defun occurN* (alat lat)
  44. (cond
  45. ((null alat) 0)
  46. ((null lat) 0)
  47. ((notatom (car alat))
  48. (+ (occurN* (car alat) lat)
  49. (occurN* (cdr alat) lat)))
  50. (t (+ (occurNa* (car alat) lat)
  51. (occurN* (cdr alat) lat)))))
  52.  
  53. (print (occurN* '(baked fried) '((fried potatoes)(baked (fried)) tomatoes)))
  54. ; 3
  55.  
  56. (print (occurN* lat1 l2))
  57. ;3
  58.  
  59. (print (occurN* lat2 l1))
  60. ;3
  61.  
  62. (print (occurN* lat1 l3))
  63. ;0
  64.  
Success #stdin #stdout 0.01s 10592KB
stdin
Standard input is empty
stdout
2 
1 
2 
3 
3 
3 
0