; Broken count function - can you fix it?
; ------------------------------
; The Little Lisper 3rd Edition
; Chapter 5
; Exercise 8
; Common Lisp
; http://t...content-available-to-author-only...r.com/thelittlelisper
; http://t...content-available-to-author-only...t.com/2010/06/little-lisper-chapter-5-multichapter.html
; http://t...content-available-to-author-only...t.com/2010/06/little-lisper.html
; ------------------------------
(setf x 'comma)
(setf y 'dot)
(setf a 'kiwis)
(setf b 'plums)
(setf lat1 '(bananas kiwis))
(setf lat2 '(peaches apples bananas))
(setf lat3 '(kiwis pears plums bananas cherries))
(setf lat4 '(kiwis mangoes kiwis guavas kiwis))
(setf l1 '((curry) () (chicken) ()))
(setf l2 '((peaches) (and cream)))
(setf l4 '())
; ------------------------------
(defun zero (n)
(= 0 n))
;Incorrect version
(defun count0 (vec)
(cond
((null vec) 1)
(t (cond
((zero (car vec))
(cons 0 (count0 (cdr vec))))
(t (count0 (cdr vec)))))))
(print (count0 (list 0 0 0 0)))
;correct version
(defun count0 (vec)
(cond
((null vec) 0)
(t (cond
((zero (car vec))
(+ 1 (count0 (cdr vec))))
(t (count0 (cdr vec)))))))
(print (count0 (list 0 0 0 0)))
;4