fork download
  1. ; Can we accumulate the potatoes in this dish?
  2. ; ------------------------------
  3. ; The Little Lisper 3rd Edition
  4. ; Chapter 6
  5. ; Exercise 9
  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.  
  19. (defun add1 (a)
  20. (+ 1 a))
  21.  
  22. (defun occur (a lat)
  23. (cond
  24. ((null lat) 0)
  25. (t (cond
  26. ((eq (car lat) a)
  27. (add1 (occur a (cdr lat))))
  28. (t (occur a (cdr lat)))))))
  29.  
  30. (print (occur 'a '(a b c d c (b a))))
  31.  
  32. (defun occur (a lat acc)
  33. (cond
  34. ((null lat) acc)
  35. (t (cond
  36. ((eq (car lat) a)
  37. (occur a (cdr lat) (add1 acc)))
  38. (t (occur a (cdr lat) acc))))))
  39.  
  40. (print (occur 'a '(a b c d c (b a)) 0))
  41.  
  42. (defun occur (a lat acc)
  43. (cond
  44. ((null lat) acc)
  45. (t (occur a (cdr lat)
  46. (cond
  47. ((eq (car lat) a)
  48. (add1 acc))
  49. (t acc))))))
  50.  
  51. (print (occur 'a '(a b c d c (b a)) 0))
  52.  
  53. ;The original value for acc is 0
  54.  
Success #stdin #stdout 0.02s 10544KB
stdin
Standard input is empty
stdout
1 
1 
1