• Re: flatten

    From B. Pym@21:1/5 to John Thingstad on Mon Aug 4 06:42:46 2025
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)