Author Topic: Subst in an only element.  (Read 2153 times)

0 Members and 1 Guest are viewing this topic.

velasquez

  • Newt
  • Posts: 195
Subst in an only element.
« on: October 08, 2009, 02:04:41 PM »
I have a list in the format -> (0 (B " C ") 0 123 0
I need to substitute (nth 2) of this list for 5. 
The new list should be -> (0 ("A" "B " C ") 5 123 0
How can I make this work?

Andrea

  • Water Moccasin
  • Posts: 2372
Re: Subst in an only element.
« Reply #1 on: October 08, 2009, 02:55:06 PM »
try this..

Code: [Select]
(defun listsubst (xlist xitem xsubst / val xlist2)
  (setq val 0)
  (setq xlist2 (mapcar '(lambda (x) (cons (setq val (1+ val)) x)) xlist))
  (setq xlist2 (subst (cons xitem xsubst) (assoc xitem xlist2) xlist2))
  (setq xlist2 (mapcar '(lambda (x) (cdr x))
                       (subst (cons xitem xsubst) (assoc xitem xlist2) xlist2)
               )
  )
)


;;test
(listsubst '(0 (B " C ") 0 123 0) 3 5)
Keep smile...

VovKa

  • Water Moccasin
  • Posts: 1630
  • Ukraine
Re: Subst in an only element.
« Reply #2 on: October 08, 2009, 03:10:01 PM »
Code: [Select]
(defun SubstNth
   (NewItem Position InList /)
  (if InList
    (if (zerop Position)
      (cons NewItem (cdr InList))
      (cons (car InList) (SubstNth NewItem (1- Position) (cdr InList)))
    )
  )
)

velasquez

  • Newt
  • Posts: 195
Re: Subst in an only element.
« Reply #3 on: October 08, 2009, 03:31:40 PM »
Thank you very much the functions work perfectly.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Subst in an only element.
« Reply #4 on: October 08, 2009, 03:45:01 PM »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.