1

I want to calculate the maximum of every sublist/level/superficial level from a list of number

Ex: (1 2 5 (4 2 7 (4 6) 9) 7 8) => (8 9 6)

What I have now is:

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)))))))

The problem is that I only wrote this program and I want to test it and on the list "---problem here" it won't add my head to the accumulation list.

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

I checked separately and (nconc some-list (list 3)) does exactly what it's supposed to... adds the number 3 to the back of the some-list. I don't know why nconc acc2 (list head) doesn't work

Tried replacing nconc with append and it's not working either. Apparently, you can't add an element to an empty list using append/nconc. Then how?

Mocktheduck
  • 1,333
  • 1
  • 22
  • 43

2 Answers2

1

A simpler implementation:

(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))))
Throw Away Account
  • 2,593
  • 18
  • 21
  • Is this supposed to return `(1 2 3)` for both `'(1 (2 (3)))` and `'(1 (2) (3))`? (It's not really clear from the OP...) – gsg Nov 29 '15 at 07:00
  • He mentioned both "sublists" and "levels." I've updated the code to operate on levels or sublists depending on a `&key` argument. – Throw Away Account Nov 29 '15 at 07:14
  • Looks great but I'm supposed to only use the basic operations of lisp so not working with trees. @gsg (1 2 3) Is the right answer. I put an example up there, you can clearly see what I mean. – Mocktheduck Nov 29 '15 at 14:30
0

Here's a (not particularly efficient) solution:

(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)
gsg
  • 9,167
  • 1
  • 21
  • 23
  • I'm not supposed to use functions like reduce or #, :, lambda. I'ma beginner and I want to solve this the most basic way I can using only lists – Mocktheduck Nov 29 '15 at 14:32