Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Nested list to nested list

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
scottbolton
990 Views, 4 Replies

Nested list to nested list

I've several functions (from others) that interrogate lists but I can't find one that can look at a nested list and rebuild a list in the same format. Confusing, sorry.

 

How to I take (setq CAPS '(("A" ("B" ("C")) ("D")) ("E"))) and using (strcase get '(("a" ("b" ("c")) ("d")) ("e"))?

 

S

4 REPLIES 4
Message 2 of 5


@scottbolton wrote:

I've several functions (from others) that interrogate lists but I can't find one that can look at a nested list and rebuild a list in the same format. Confusing, sorry.

 

How to I take (setq CAPS '(("A" ("B" ("C")) ("D")) ("E"))) and using (strcase get '(("a" ("b" ("c")) ("d")) ("e"))?

 

S


(defun map-tree (func tree)
  (cond
    ((null tree) nil)
    ((vl-consp tree)
     (cons (map-tree func (car tree))
           (map-tree func (cdr tree))))
    (T (func tree))))

 _$ (map-tree (lambda (s)(strcase s t)) caps)
(("a" ("b" ("c")) ("d")) ("e"))

 

 _$ (map-tree ascii caps)
((65 (66 (67)) (68)) (69))

 

_$ (map-tree sqrt (cons 56 89))
(7.48331 . 9.43398)

 

--

 

Message 3 of 5

Thanks, Martti. This looks good. I'll need to see how I can modify this to perform a more complex "func" - the (strcase example was simplified.

 

If I run in to problems I'll be back!

 

S

Message 4 of 5


@scottbolton wrote:

 I'll need to see how I can modify this to perform a more complex "func" - the (strcase example was simplified.

 


This should work for any single-argument function, as long as it produces a result and doesn't crash.

 

--

 

Message 5 of 5

Martti,

 

So this (very simple) function is what I have:

 

(defun tester (arg)
 (cons arg (strcase arg T))
 )
(map-tree tester '(("A" ("B" ("C")) ("D")) ("E")))

'((("A" . "a") (("B" . "b") (("C" . "c"))) (("D" . "d"))) (("E" . "e")))

 

Now I can start to go places with it. Thanks for your help.

 

S

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost