Author Topic: deep insert item in list  (Read 1998 times)

0 Members and 1 Guest are viewing this topic.

Vandyck

  • Newt
  • Posts: 24
deep insert item in list
« on: May 31, 2015, 04:52:02 AM »
I'm trying to create a function to insert an item in a list in deep position.
example:
I have this  list:
(("zzz" ("abc" ("www")) ("qqq" ("ccc" ("bbb"))))))

I want to add/insert new list ("111" "222" "333") in ("bbb" )

to obtain this:
(("zzz" ("abc" ("www")) ("qqq" ("ccc" ("bbb" ("111" "222" "333")))))))

I can not find a good way to rebuild the sublists inside list.
Note that list items values are unique, never duplicate.
this is my code:
Code - Auto/Visual Lisp: [Select]
  1. (setq out '() )
  2. (defun deepinsert (lst upd new)
  3.   (cond ((null lst) nil)
  4.  
  5.         ((atom (nth 0 lst))
  6.          (if (equal (nth 0 lst) upd)
  7.              (setq out(cons (cons (nth 0 lst) (list new)) out))
  8.            (progn
  9.              (setq out(cons (nth 0 lst) out))
  10.              (deepinsert (cdr lst) upd new)
  11.            )  
  12.          )
  13.         )
  14.         (T
  15.          (or (deepinsert (nth 0 lst) upd new)
  16.              (deepinsert (cdr lst) upd new)
  17.          )
  18.         )
  19.   )
  20.   out
  21. )
  22. (deepinsert '(("zzz" ("abc" ("www")) ("qqq" ("ccc" ("bbb"))))) "bbb" '("111" "222"))
  23. =>  ("www" "abc" "zzz" ("ccc" ("111" "222")) "bbb" "aaa")
  24.  
« Last Edit: May 31, 2015, 04:58:23 AM by Vandyck »

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: deep insert item in list
« Reply #1 on: May 31, 2015, 05:17:36 AM »
Here's one way:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( new key lst )
  2.     (apply 'append
  3.         (mapcar
  4.            '(lambda ( x )
  5.                 (if (atom x)
  6.                     (if (= key x) (list x new) (list x))
  7.                     (list (foo new key x))
  8.                 )
  9.             )
  10.             lst
  11.         )
  12.     )
  13. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (foo '("111" "222" "333") "bbb" '(("zzz" ("abc" ("www")) ("qqq" ("ccc" ("bbb"))))))
  2. (("zzz" ("abc" ("www")) ("qqq" ("ccc" ("bbb" ("111" "222" "333"))))))

Vandyck

  • Newt
  • Posts: 24
Re: deep insert item in list
« Reply #2 on: May 31, 2015, 08:07:06 AM »
thanks LEE !
You are always quick to find elegant solutions.
 :-D

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: deep insert item in list
« Reply #3 on: May 31, 2015, 08:24:39 AM »
You're welcome  :-)

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: deep insert item in list
« Reply #4 on: June 03, 2015, 04:23:15 AM »
Alternative solution. Not to be used with reals (loss of accuracy).
Code - Auto/Visual Lisp: [Select]
  1. ; (insertInList '("111" "222" "333") "bbb" '(("zzz" ("abc" ("www")) ("qqq" ("ccc" ("bbb"))))))
  2. ;   => (("zzz" ("abc" ("www")) ("qqq" ("ccc" ("bbb" ("111" "222" "333"))))))
  3. (defun insertInList (new key lst)
  4.   (read
  5.     (vl-string-subst
  6.       (strcat (vl-prin1-to-string key) " " (vl-prin1-to-string new))
  7.       (vl-prin1-to-string key)
  8.       (vl-prin1-to-string lst)
  9.     )
  10.   )
  11. )