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
(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))
)
)
)