Author Topic: Find the nest level of a list  (Read 7085 times)

0 Members and 1 Guest are viewing this topic.

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: Find the nest level of a list
« Reply #30 on: October 20, 2017, 12:22:52 PM »
could it be that there's a misprint in the book, shouldn't it be -1?
and there is no need to define max_ function, we can use built-in max instead
Code: [Select]
(defun depth (x)
  (cond ((atom x) -1)
(t (max (1+ (depth (car x))) (depth (cdr x))))
  )
)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Find the nest level of a list
« Reply #31 on: October 20, 2017, 12:36:04 PM »

could it be that there's a misprint in the book, shouldn't it be -1?
and there is no need to define max_ function, we can use built-in max instead
Code: [Select]
(defun depth (x)
  (cond   ((atom x) -1)
   (t (max (1+ (depth (car x))) (depth (cdr x))))
  )
)
Code: [Select]
(setq alist '(((((("A") ("a\"(((())))") ((1 . 2))  ))((((((((((((((((((((((("B" ("C"))))))))))))))))))))))))("C")))) ); -> 26
Elapsed milliseconds / relative speed for 65536 iteration(s):
    (DEPTH ALIST).....2000 / 1.53 <fastest>
    (FOO#5 ALIST).....3063 / 1 <slowest>

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Find the nest level of a list
« Reply #32 on: October 20, 2017, 01:03:37 PM »
Which is it?
Code - Auto/Visual Lisp: [Select]
  1. _$ (depth '())
  2. -1
  3. _$ (foo '())
  4. 0

JohnK

  • Administrator
  • Seagull
  • Posts: 10634
Re: Find the nest level of a list
« Reply #33 on: October 20, 2017, 01:49:19 PM »
I havent read the thread but I don't think that classifies as a tree, Lee. However, I guess it's depth would technically be zero if it were classified as one. But I think I would still want my function to return -1 in the case of a null tree.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Find the nest level of a list
« Reply #34 on: October 20, 2017, 02:26:42 PM »
Damn I'm so close fixing this (iterative) code from reply #13:

Code - Auto/Visual Lisp: [Select]
  1. (defun NestLvl ( L / i r )
  2.   (and L (listp L) (setq i 0)
  3.     (while (and L (vl-some (function vl-consp) L))
  4.       (setq L
  5.         (apply (function append)
  6.           (setq r ; only for debugging purpose
  7.             (mapcar
  8.               (function
  9.                 (lambda (x / v )
  10.                   (cond
  11.                     ( (atom x) nil)
  12.                     (  
  13.                       (if (atom (setq v (cdr x))) ; dotted pair
  14.                         (cons (car x) (list v))
  15.                         x
  16.                       )
  17.                     )
  18.                   ); cond
  19.                 ); lambda
  20.               ); function
  21.               L
  22.             ); mapcar
  23.           )
  24.         )
  25.       )
  26.       (setq i (1+ i))
  27.     )
  28.   )
  29.   i
  30. ); defun NestLvl

Code - Auto/Visual Lisp: [Select]
  1. _$ (setq aL
  2.   '(
  3.     ((1 . 2))
  4.     ("a\"(((())))")
  5.     "string"
  6.     nil
  7.     ("A" "B" "C")
  8.     (("A")("B")("C"))
  9.     (("A")(("B"))("C"))
  10.     (("A")((((("B")))))("C"))
  11.     (((("A")))((((((("B")))))))("C"))
  12.     (((("A")))((((((((((((((((((((((("B" ("C"))))))))))))))))))))))))("C"))
  13.     (((((("A")))((((((((((((((((((((((("B" ("C"))))))))))))))))))))))))("C"))))
  14.     (("(1") "2" "3")
  15.     (((1) (2) (3)))
  16.     ("a\"(((())))")
  17.   )
  18. )
  19.  
  20. _$ (mapcar 'NestLvl aL)
  21. (1 0 nil nil 0 1 2 5 7 24 26 1 2 0)
  22. _$ (mapcar 'NestedListLevel aL)
  23. (0 0 nil nil 0 1 2 5 7 24 26 1 2 0) ; this is the correct result for reference
Yet so far.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Find the nest level of a list
« Reply #35 on: October 23, 2017, 10:54:07 AM »
; Just for fun - iterative - string based
; (mapcar 'ALE_List_NestedLevel3 aL) > (1 0 0 0 0 1 2 5 7 24 26 1 2 0)
Code: [Select]
(defun ALE_List_NestedLevel4 (l / r s e c i o m)
  (setq i (vl-prin1-to-string l)  s 0  c -1  o 0)
  (while (vl-string-position 92 i) (setq i (vl-string-subst "" "\\\"" i)))
  (while (setq e (vl-string-position 41 i s))
    (cond
      ( (= "\"" (setq r (substr i (setq s (1+ s)) 1))) (setq s (1+ (vl-string-position 34 i s))) )
      ( (= "(" r) (setq c (1+ c)) )
      ( (= ")" r) (setq o (max c o)  c (1- c)) )
      ( (or (not (setq m (vl-string-position 40 i (1- s)))) (> m e)) (setq s e) )
    )
  )
  o
)

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: Find the nest level of a list
« Reply #36 on: October 23, 2017, 05:10:38 PM »
Code: [Select]
(ALE_List_NestedLevel4 '("\\\""))

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Find the nest level of a list
« Reply #37 on: October 24, 2017, 03:04:13 PM »
Code: [Select]
(ALE_List_NestedLevel4 '("\\\""))
...maybe it's time to throw the sponge...