fork download
  1. ; Broken count function - can you fix it?
  2. ; ------------------------------
  3. ; The Little Lisper 3rd Edition
  4. ; Chapter 5
  5. ; Exercise 8
  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 zero (n)
  25. (= 0 n))
  26.  
  27. ;Incorrect version
  28. (defun count0 (vec)
  29. (cond
  30. ((null vec) 1)
  31. (t (cond
  32. ((zero (car vec))
  33. (cons 0 (count0 (cdr vec))))
  34. (t (count0 (cdr vec)))))))
  35.  
  36. (print (count0 (list 0 0 0 0)))
  37.  
  38. ;correct version
  39. (defun count0 (vec)
  40. (cond
  41. ((null vec) 0)
  42. (t (cond
  43. ((zero (car vec))
  44. (+ 1 (count0 (cdr vec))))
  45. (t (count0 (cdr vec)))))))
  46.  
  47. (print (count0 (list 0 0 0 0)))
  48. ;4
  49.  
Success #stdin #stdout 0s 10592KB
stdin
Standard input is empty
stdout
(0 0 0 0 . 1) 
4