如何在 LISP 中查找列表的子列表(来自每个级别)的数量?

How do I find the number of sublists (from every level) of a list in LISP?

例如。对于列表 (A 1 (B 2) (1 C 4) (D 1 (6 F)) ((G 4) 6)) => 6

一个快速的答案是:

(defun count-sublists (list)
  (if (listp list)
      (1+ (loop for el in list
             sum (count-sublists el)))
      0))

只需遍历元素并递归计算所有列表。