From Newsgroup: comp.lang.lisp
John Thingstad wrote:
As a sidenote I played around with ways of counting leaves
(defun count-leaves (list)
(loop for element in list summing
(if (listp element) (count-leaves element) 1)))
(defun count-leaves (element)
(typecase element
(list (reduce #'+ (mapcar #'count-leaves element)))
(atom 1)))
Here I clearly prefer the first as it is MUCH more efficient (and also slightly terser).
This is slightly faster than the LOOP version.
(defun count-leaves3 (seq)
(do ((sum 0
(let ((e (pop seq)))
(+ sum (if (listp e)(count-leaves3 e) 1)))))
((not seq) sum)))
--
[T]he problem is that lispniks are as cultish as any other devout group and basically fall down frothing at the mouth if they see [heterodoxy].
--- Kenny Tilton
The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham --- Synchronet 3.21a-Linux NewsLink 1.2