List 每个级别(表面级别)LISP上的最大数量

List 每个级别(表面级别)LISP上的最大数量,list,lisp,max,common-lisp,clisp,List,Lisp,Max,Common Lisp,Clisp,我想从数字列表中计算每个子列表/级别/表面级别的最大值 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 f

我想从数字列表中计算每个子列表/级别/表面级别的最大值

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
我单独检查了一下,
(ncoc some list(list 3))
做的正是它应该做的。。。将数字3添加到some列表的后面。我不知道为什么
NCOC acc2(列表头)
不起作用


尝试用append替换nconc,但它也不起作用。显然,不能使用append/ncoc将元素添加到空列表中。那么如何实现呢?

一个更简单的实现:

(defun max-superficial/sublists (list)
  (loop for num in list
       if (listp num) append (max-superficial/sublists num) into sublists
       else if (numberp num) maximize num into max
       else do (error "Not a number or list: ~a" num)
       finally (return (cons max sublists))))

;; If you want the max of each "level" or depth in a tree,
;; then you need to be able to operate on levels. Here are some
;; functions that are analogous to FIRST, REST, and POP:

(defun top-level (tree)
  (remove-if-not #'numberp tree))

(defun rest-levels (tree)
  (apply #'append (remove-if-not #'listp tree)))

(defmacro pop-level (tree)
  `(let ((top (top-level ,tree)))
     (setf ,tree (rest-levels ,tree))
     top))

(defun max-superficial (tree &key use-sublists)
  "It wasn't clear if you wanted the max in each sublist or the max
at each depth, so both are implemented. Use the :use-sublists key
to get the max in each sublist, otherwise the max at each depth
will be computed."
  (if use-sublists
      (max-superficial/sublists tree)
      (loop for top-level = (pop-level tree)
         collect (if top-level (reduce #'max top-level)) into result
         unless tree do (return result))))
以下是一个(不是特别有效)的解决方案:

(defun max-avoiding-nil (a b)
  (cond ((null a) b)
    ((null b) a)
    (t (max a b))))

(defun depth-maximum (a b)
  (cond ((null a) b)
    ((null b) a)
    (t
     (cons (max-avoiding-nil (car a) (car b))
           (depth-maximum (cdr a) (cdr b))))))

(defun tree-max-list (list depth)
  (reduce #'depth-maximum tree
          :key (lambda (elt) (tree-max elt depth))
          :initial-value '()))

(defun tree-max (tree depth)
  (if (listp tree)
      (tree-max-list tree (1+ depth))
    (append (make-list depth 'nil) (list tree))))

(defun tree-maximums (tree)
  (tree-max-list tree 0))

(tree-maximums '(1 2 5 (4 2 7 (4 6) 9) 7 8)) => (8 9 6)
(tree-maximums '()) => nil
(tree-maximums '(1)) => (1)
(tree-maximums '((2))) => (nil 2)
(tree-maximums '((2) (3))) => (nil 3)

我现在在手机上,无法详细阅读您的所有代码,但请注意,NCOC具有破坏性:它修改了除最后一个参数以外的所有参数中的最后一个cdr。您还使用了一些引用列表。这可能会导致一些意外的结果。请参见,例如,将
ncoc
替换为
append
是否解决了您的问题?不,这并不能解决问题。我不知道这个问题对我有什么帮助。我的NCOC将什么都不做,也不做。对于
'(1(2(3))
'(1(2)(3))
,这是否应该返回
(1(2)(3))
?(从OP中看不太清楚…)他提到了“子列表”和“级别”。我已经更新了代码,根据
&key
参数对级别或子列表进行操作。看起来不错,但我应该只使用lisp的基本操作,所以不使用树@gsg(123)是正确的答案。我举了一个例子,你可以清楚地看到我的意思。我不应该使用像reduce或#,:,lambda这样的函数。我是初学者,我想用我能用的最基本的方法来解决这个问题