Искам да изчисля максимума на всеки подсписък/ниво/повърхностно ниво от списък с числа
Ex: (1 2 5 (4 2 7 (4 6) 9) 7 8) => (8 9 6)
Това, което имам сега е:
maximum (l) ;;function to compute the maximum number for a simple list, it works
(defun max-superficial (lista acc acc2) ;;main function: lista - my list, acc - my final list
;;of results, acc2 - accumulation list for a sublist
(typecase lista
(null
(typecase acc2
;; if my list is empty and I have nothing accumulated, just return the final list
(null acc)
;;if my list is empty but I have something in my accumulation list, just add the maximum
;;of acc2 to my final list
(t (nconc acc (list (maximum acc2))))))
(cons (destructuring-bind (head . tail) lista
(typecase head
(list
;;if my list isn't empty and the head of the list is a list itself, call
;;the function again for the head with an empty accumulation list and then call it again
;;for the tail
(nconc acc
(list (max-superficial head acc nil))
(max-superficial tail acc acc2)))
;; otherwise just accumulate the head and call the function for the tail
---problem here (t (nconc acc2 (list head))
(print '(wtf))
(print acc)
(print acc2)
(print head)
(max-superficial tail acc acc2)))))))
Проблемът е, че написах само тази програма и искам да я тествам и в списъка "---проблем тук" няма да добави главата ми към списъка за натрупване.
For: (max-superficial '(1 2) nil nil) --result should be ==> wtf nil (1) 1 wtf nil (1 2) 2 2
My result: wtf nil nil 1 wtf nil nil 2 nil
Проверих отделно и (nconc some-list (list 3))
прави точно това, което трябва... добавя числото 3 в задната част на списъка с някои. Не знам защо nconc acc2 (list head)
не работи
Опитах да заменя nconc с append и също не работи. Очевидно не можете да добавите елемент към празен списък с помощта на append/nconc. Тогава как?
nconc
сappend
решава ли проблема ви? - person sds   schedule 29.11.2015