Code Red > AutoLISP (Vanilla / Visual)

Find the nest level of a list

<< < (2/8) > >>

Lee Mac:
Thanks Grrr1337  :-)

Here's another:
--- Code - Auto/Visual Lisp: ---(defun foo ( l )    (cond        (   (/= 'list (type l)) 0)        (   (listp (car l)) (max (+ 1 (foo (car l))) (foo (cdr l))))        (   (foo (cdr l)))    ))

Lee Mac:
Another, RJP-style :-)


--- Code - Auto/Visual Lisp: ---(defun foo ( l / n r x )    (setq l (vl-string->list (vl-prin1-to-string l)) r 0 n -1)    (while (setq x (car l))        (cond            (   (= 34 x) (setq l (member 34 (cdr l))))            (   (= 40 x) (setq n (1+ n)))            (   (= 41 x) (setq r (max n r) n (1- n)))        )        (setq l (cdr l))    )    r)

Grrr1337:
Lee, the second one is outstanding! :-o
As I understood,
First you dig into the levels and then the recursion 'exits out'
by comparing each pair of lists (where the more nested has a higher priority for the max function [due the sum of the increasing 1+ increment]).

Even if I had the thought just for the idea I would be unable to write it correctly.
Actually now I see that in your both codes you are able to avoid to pass an index variable.
(BTW such complex subfunctions hint themselves about their author, even if its not mentioned)

Lee Mac:

--- Quote from: Grrr1337 on October 18, 2017, 03:49:37 PM ---Lee, the second one is outstanding! :-o
As I understood,
First you dig into the levels and then the recursion 'exits out'
by comparing each pair of lists (where the more nested has a higher priority for the max function [due the sum of the increasing 1+ increment]).
--- End quote ---

Thank you Grrr1337 - the recursion is perhaps best demonstrated by a trace call:

--- Code - Auto/Visual Lisp: ---_$ (foo '(("A")((((("B")))))("C")))5
--- Code - Auto/Visual Lisp: ---Entering (FOO (("A") ((((("B"))))) ("C")))  Entering (FOO ("A"))    Entering (FOO nil)    Result:  0  Result:  0  Entering (FOO (((((("B"))))) ("C")))    Entering (FOO ((((("B"))))))      Entering (FOO (((("B")))))        Entering (FOO ((("B"))))          Entering (FOO (("B")))            Entering (FOO ("B"))              Entering (FOO nil)              Result:  0            Result:  0            Entering (FOO nil)            Result:  0          Result:  1          Entering (FOO nil)          Result:  0        Result:  2        Entering (FOO nil)        Result:  0      Result:  3      Entering (FOO nil)      Result:  0    Result:  4    Entering (FOO (("C")))      Entering (FOO ("C"))        Entering (FOO nil)        Result:  0      Result:  0      Entering (FOO nil)      Result:  0    Result:  1  Result:  5Result:  5

--- Quote from: Grrr1337 on October 18, 2017, 03:49:37 PM ---Even if I had the thought just for the idea I would be unable to write it correctly.
Actually now I see that in your both codes you are able to avoid to pass an index variable.
(BTW such complex subfunctions hint themselves about their author, even if its not mentioned)
--- End quote ---

That's very kind of you to say, but don't be so down on yourself - your programming abilities have already improved significantly over the last few months since you started participating on the forums, and so with continued study & practice, there's no reason why you can't continue to progress. :-)

ronjonp:

--- Quote from: Lee Mac on October 18, 2017, 03:18:17 PM ---
--- Code - Auto/Visual Lisp: ---(nestedlistlevel_rjp '(("(1") "2" "3"))

--- Code - Auto/Visual Lisp: ---(nestedlistlevel_rjp '(((1) (2) (3))))
 ;)

--- End quote ---
I figured it was a bust :) .. just a quick one for the data set provided.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version