Я хочу рассчитать максимум каждого подсписка/уровня/поверхностного уровня из списка номеров
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