Author Topic: Replace nth member of a list  (Read 20023 times)

0 Members and 1 Guest are viewing this topic.

chlh_jd

  • Guest
Re: Replace nth member of a list
« Reply #60 on: December 24, 2012, 01:14:52 AM »
@Irneb, for a little extra concision, this:
Code - Auto/Visual Lisp: [Select]
  1. (cons (cadddr Source) (cons (caddr Source) (cons (cadr Source) (cons (car Source) Result))))

could become:
Code - Auto/Visual Lisp: [Select]
  1. (vl-list* (cadddr Source) (caddr Source) (cadr Source) (car Source) Result)

I'm unsure of any performance benefit however.

...

Benchmarking ..............Elapsed milliseconds / relative speed for 2048 iteration(s):

    (VL-LIST*-IT L).....1107 / 1.25 <fastest>
    (CONS-IT L).........1389 / 1.00 <slowest>[/code]

Nice method , Lee .

Lee Mac

  • Seagull
  • Posts: 12502
  • London, England
Re: Replace nth member of a list
« Reply #61 on: December 24, 2012, 08:31:30 AM »

chlh_jd

  • Guest
Re: Replace nth member of a list
« Reply #62 on: December 26, 2012, 01:00:57 AM »
Thanks Evgeniy,
You can now see how the position of the index in the list affects the time required.
Also the compiling surprised me as I thought it would be a uniform change in the speed & not the order.
Funny how some methods fair better when compiled.

I wish to give you a small gift by new year...  :-)  :-)  :-)

 :-)
I congratulate on coming new year!
Let new year will bring a lot of pleasure and happiness!
Happy New Year!
 :-)

Nice suggest , ElpanovEvgeniy  .
I think we are all get the gift ! :-)
Happy New Year !

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1544
  • Moscow (Russia)
Re: Replace nth member of a list
« Reply #63 on: December 26, 2012, 03:17:43 AM »

Code - Auto/Visual Lisp: [Select]
  1. (vl-list* (cadddr Source) (caddr Source) (cadr Source) (car Source) Result)


very good idea!  :-)
Stay home. Stay safe. Save lives.

CADDOG

  • Newt
  • Posts: 77
  • wishbonesr
Re: Replace nth member of a list
« Reply #64 on: December 28, 2012, 01:02:49 PM »
...for you consideration

LeeMac-replace3

This simplistic version holds up against the verbose and non-verbose on the bench.  And is the reason I use it every time.

Inerb's progressive bench floated replace3 to the top twice after 500.  Just saying.   :angel:

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1199
  • Marco
Re: Replace nth member of a list
« Reply #65 on: January 10, 2013, 08:21:32 AM »
Sorry to revive this ancient thread, just figured my version seems to outperform all the others in nearly every instance

I apologize also for my late... (see attached).
 
I = 950
Benchmarking ......... done for 1024 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(ALE_LIST_SUBSTNTH I A *LIST@ (REVER...)      1024      1809      1809      6.55
(REPLACE_CAB *LIST@ I A)                                  512      1138      2276      5.21
(IB:REPLACE-NTH *LIST@ I A)                            1024      2387      2387      4.96
(REPLASE *LIST@ I A)                                          512      1732      3464      3.42
(REPLACE_DA A I *LIST@)                                    512      2152      4304      2.75
(REPLASE_1 *LIST@ I A)                                      256      1437      5748      2.06
(NTH-REPLACE I A *LIST@)                                  256      1466      5864      2.02
(REPLASE_2 *LIST@ I A)                                      256      1949      7796      1.52
(SWAPNTH *LIST@ I A)                                        128      1481     11848      1.00
--------------------------------------------------------------------------------1050
Code: [Select]
(defun ALE_List_SubstNth (NthPos NewItm In_Lst InRLst / LstLng OldItm)
  (cond
    ( (null In_Lst)                                nil )
    ( (zerop NthPos)        (cons NewItm (cdr In_Lst)) )
    ( (<= (setq LstLng (length In_Lst)) NthPos) In_Lst )
    ( (zerop (setq LstLng (- LstLng (1+ NthPos))))
      (append (reverse (cdr InRLst)) (list NewItm))
    )
    ( T
      (setq OldItm (nth NthPos In_Lst))
      (while
        (/=
          NthPos
          (length (setq InRLst (cdr (member OldItm InRLst))))
        )
      )
      (while
        (/=
          LstLng
          (length (setq In_Lst (cdr (member OldItm In_Lst))))
        )
      )
      (append (reverse InRLst) (cons NewItm In_Lst))
    )
  )
)