Author Topic: how to replace at item in a list  (Read 12178 times)

0 Members and 1 Guest are viewing this topic.

RAIN CODE

  • Guest
how to replace at item in a list
« on: May 08, 2012, 06:36:49 AM »

Hi,

 I have a problem here hope someone can help.
Example a list - (list 1 2 2 3 3 1 5 4 9 3) and I want to replace the 2nd item of the list with 6.
            results  (list 1 6 2 3 3 1 5 4 9 3)
Is there any preset Lisp function that can replace an item in a list. I used subst but it replace others too which I wanted it unchange.
I have written a lisp for it but the lisp is long and slow down the game when the main program call this subroutine Lisp too often.

Thanks

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: how to replace at item in a list
« Reply #1 on: May 08, 2012, 06:44:03 AM »

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: how to replace at item in a list
« Reply #2 on: May 08, 2012, 07:34:13 AM »
My version ..  :-)
Code - Auto/Visual Lisp: [Select]
  1. (defun _ReplaceItem (place item lst / lol i)
  2. ;;;             Tharwat 08. May 2012                  ;
  3. ;;; place = Place of item to be replaced              ;
  4. ;;; item  = the new item to take a place              ;
  5. ;;; lst   = list of items to implement the changes on ;
  6.   (if (and lst (> (length lst) place))
  7.     (progn
  8.       (setq i 0)
  9.       (repeat (length lst)
  10.         (setq lol (cons (if (eq place (1+ i))
  11.                           item
  12.                           (nth i lst)
  13.                         )
  14.                         lol
  15.                   )
  16.         )
  17.         (setq i (1+ i))
  18.       )
  19.     )
  20.   )
  21.   (reverse lol)
  22. )


usage of function ..

Code - Auto/Visual Lisp: [Select]
  1. (_ReplaceItem 2 6 (list 1 2 2 3 3 1 5 4 9 3))

another with Strings ...

Code - Auto/Visual Lisp: [Select]
  1. (_ReplaceItem 2 "AutoLISP" (list 1 2 2 3 3 1 5 4 9 3))

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: how to replace at item in a list
« Reply #3 on: May 08, 2012, 10:59:19 AM »
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.

RAIN CODE

  • Guest
Re: how to replace at item in a list
« Reply #4 on: May 25, 2012, 06:01:29 AM »
many thanks guys for your help

you all are the Jedi master. I feel like a newbie when i read thru' the lisp here.


CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: how to replace at item in a list
« Reply #5 on: May 25, 2012, 07:35:04 AM »
You're welcome.
Keep hanging out.  8-)
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.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: how to replace at item in a list
« Reply #6 on: May 25, 2012, 09:56:11 AM »
My version:
Code - Auto/Visual Lisp: [Select]
  1. (defun replace-n (new n lst / )
  2.   (mapcar '(lambda (a) (if (= (setq n (1- n)) -1) new a)) lst))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: how to replace at item in a list
« Reply #7 on: May 25, 2012, 02:44:39 PM »
Or to use the CL (Common Lisp) replace method: http://alisp-ext.wikidot.com/auotolisp-replace

It could probably be done better / faster / more elegant. You might also like some of the others:
http://alisp-ext.wikidot.com/autolisp
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pBe

  • Bull Frog
  • Posts: 402
Re: how to replace at item in a list
« Reply #8 on: May 27, 2012, 09:11:31 AM »
just to be on the mix:

Code - Auto/Visual Lisp: [Select]
  1. (defun _rep  (lst i v / l)
  2.               (repeat i
  3.                     (setq l   (cons (Car lst) l)
  4.                           lst (cdr lst)))
  5.               (apply 'append (list (reverse l) (list v) (cdr lst))))


irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: how to replace at item in a list
« Reply #9 on: May 28, 2012, 06:10:35 AM »
That's probably a lot more efficient than mine. How about this one as well:
Code - Auto/Visual Lisp: [Select]
  1. (defun replaceN  (new N lst / result)
  2.   (cond ((or (< N 0) (>= N (length lst))) lst)
  3.         ((< N (/ (length lst) 2))
  4.          (repeat n
  5.            (setq result (cons (car lst) result)
  6.                  lst    (cdr lst)))
  7.          (append (reverse result) (list new) (cdr lst)))
  8.         (t
  9.          (setq lst (reverse lst))
  10.          (repeat (- (length lst) n)
  11.            (setq result (cons (car lst) result)
  12.                  lst    (cdr lst)))
  13.          (append (reverse lst) (list new) (cdr result)))))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.