(defun calculate-level-sums (lst) (prepare-result (calculate-level-sums-helper lst 1 '()))) (defun calculate-level-sums-helper (lst level acc) (cond ((null lst) acc) (t (let ((head (car lst)) (tail (cdr lst))) (multiple-value-bind (new-acc remaining-tail) (process-item head level acc) (calculate-level-sums-helper tail level new-acc)))))) (defun process-item (item level acc) (cond ((numberp item) (values (update-level-sum acc level item) nil)) ((listp item) (values (calculate-level-sums-helper item (1+ level) acc) nil)) (t (values acc nil)))) (defun update-level-sum (acc level value) (let ((existing (assoc level acc))) (if existing (let ((new-acc (remove existing acc :test #'equal))) (cons (list level (+ value (cadr existing))) new-acc)) (cons (list level value) acc)))) (defun prepare-result (acc) (if (assoc 1 acc) (sort acc #'< :key #'car) (sort (cons '(1 0) acc) #'< :key #'car))) ;; Тесты (format t "~a~%" (calculate-level-sums '(a (b (4 (2 e (3) k 15) e 5) 7)))) ; ((1 0) (2 7) (3 9) (4 17) (5 3)) (format t "~a~%" (calculate-level-sums '(a b c))) ; ((1 0)) (format t "~a~%" (calculate-level-sums '(1 (2 (3))))) ; ((1 1) (2 2) (3 3)) (format t "~a~%" (calculate-level-sums'(1 (2 3 (4 (5 6)))))) ; ((1 1) (2 5) (3 4) (4 11))
Standard input is empty
((1 0) (2 7) (3 9) (4 17) (5 3)) ((1 0)) ((1 1) (2 2) (3 3)) ((1 1) (2 5) (3 4) (4 11))
Warning: reserving address range 0x80000c0000...0x1fffffffffff that contains memory mappings. clisp might crash later! Memory dump: 0x8000000000 - 0x80000bffff 0x1522f1600000 - 0x1522f18e4fff 0x1522f1a15000 - 0x1522f1a39fff 0x1522f1a3a000 - 0x1522f1bacfff 0x1522f1bad000 - 0x1522f1bf5fff 0x1522f1bf6000 - 0x1522f1bf8fff 0x1522f1bf9000 - 0x1522f1bfbfff 0x1522f1bfc000 - 0x1522f1bfffff 0x1522f1c00000 - 0x1522f1c02fff 0x1522f1c03000 - 0x1522f1e01fff 0x1522f1e02000 - 0x1522f1e02fff 0x1522f1e03000 - 0x1522f1e03fff 0x1522f1e80000 - 0x1522f1e8ffff 0x1522f1e90000 - 0x1522f1ec3fff 0x1522f1ec4000 - 0x1522f1ffafff 0x1522f1ffb000 - 0x1522f1ffbfff 0x1522f1ffc000 - 0x1522f1ffefff 0x1522f1fff000 - 0x1522f1ffffff 0x1522f2000000 - 0x1522f2003fff 0x1522f2004000 - 0x1522f2203fff 0x1522f2204000 - 0x1522f2204fff 0x1522f2205000 - 0x1522f2205fff 0x1522f22c1000 - 0x1522f22c4fff 0x1522f22c5000 - 0x1522f22c5fff 0x1522f22c6000 - 0x1522f22c7fff 0x1522f22c8000 - 0x1522f22c8fff 0x1522f22c9000 - 0x1522f22c9fff 0x1522f22ca000 - 0x1522f22cafff 0x1522f22cb000 - 0x1522f22d8fff 0x1522f22d9000 - 0x1522f22e6fff 0x1522f22e7000 - 0x1522f22f3fff 0x1522f22f4000 - 0x1522f22f7fff 0x1522f22f8000 - 0x1522f22f8fff 0x1522f22f9000 - 0x1522f22f9fff 0x1522f22fa000 - 0x1522f22fffff 0x1522f2300000 - 0x1522f2301fff 0x1522f2302000 - 0x1522f2302fff 0x1522f2303000 - 0x1522f2303fff 0x1522f2304000 - 0x1522f2304fff 0x1522f2305000 - 0x1522f2332fff 0x1522f2333000 - 0x1522f2341fff 0x1522f2342000 - 0x1522f23e7fff 0x1522f23e8000 - 0x1522f247efff 0x1522f247f000 - 0x1522f247ffff 0x1522f2480000 - 0x1522f2480fff 0x1522f2481000 - 0x1522f2494fff 0x1522f2495000 - 0x1522f24bcfff 0x1522f24bd000 - 0x1522f24c6fff 0x1522f24c7000 - 0x1522f24c8fff 0x1522f24c9000 - 0x1522f24cefff 0x1522f24cf000 - 0x1522f24d1fff 0x1522f24d4000 - 0x1522f24d4fff 0x1522f24d5000 - 0x1522f24d5fff 0x1522f24d6000 - 0x1522f24d6fff 0x1522f24d7000 - 0x1522f24d7fff 0x1522f24d8000 - 0x1522f24d8fff 0x1522f24d9000 - 0x1522f24dffff 0x1522f24e0000 - 0x1522f24e2fff 0x1522f24e3000 - 0x1522f24e3fff 0x1522f24e4000 - 0x1522f2504fff 0x1522f2505000 - 0x1522f250cfff 0x1522f250d000 - 0x1522f250dfff 0x1522f250e000 - 0x1522f250efff 0x1522f250f000 - 0x1522f250ffff 0x55e15e660000 - 0x55e15e750fff 0x55e15e751000 - 0x55e15e85afff 0x55e15e85b000 - 0x55e15e8bafff 0x55e15e8bc000 - 0x55e15e8eafff 0x55e15e8eb000 - 0x55e15e91bfff 0x55e15e91c000 - 0x55e15e91ffff 0x55e160281000 - 0x55e1602a1fff 0x7ffc8ff2b000 - 0x7ffc8ff4bfff 0x7ffc8ff8f000 - 0x7ffc8ff92fff 0x7ffc8ff93000 - 0x7ffc8ff94fff