TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Keith™ on December 27, 2006, 10:24:32 AM

Title: Replace nth member of a list
Post by: Keith™ on December 27, 2006, 10:24:32 AM
Ok, I am having a serious brain fart this morning .. maybe because I have had too much egg nog or tequila .. your choice ... anyway, I need a clean and effective method to replace the nth member of a list. Example:

Code: [Select]
(setq newlist (replace index oldlist newitem))

If I had a dotted pair I could use a simple subst, but unfortunately I don't have one and it would likely be a nightmare since it would be possible to have 2 identical pairs in the list.
Title: Re: Replace nth member of a list
Post by: CAB on December 27, 2006, 10:33:43 AM
Here is my shot at it.

Code: [Select]
(defun c:test ()
  (setq lst '(0 1 2 3 4 5 6))
  (setq new (swapnth lst 3 "A"))
)

Code: [Select]
;; CAB 11/15/2006
;;  replace nth item in list
(defun swapnth (lst i1 itm / tmp nlst)
  (while (and (> i1 0)
              (< (length (setq nlst (cons (car lst) nlst))) i1))
    (setq lst (cdr lst))
    )
  (setq nlst (cons itm nlst)
        lst (cddr lst))
  (while (or (setq tmp (car lst)) lst)
    (setq nlst (cons tmp nlst)
          lst (cdr lst))
    )
  (reverse nlst)
)
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 27, 2006, 10:44:33 AM
Code: [Select]
(defun replase (lst i itm)
  ;;(replase '(0 1 2 3 4 5 6) 3 "A")
  ;; =>
  ;; '(0 1 2 "A" 4 5 6)
  (mapcar
    (function
      (lambda (x)
        (if (zerop i)
          (progn (setq i (1- i)) itm)
          (progn (setq i (1- i)) x)
        ) ;_ if
      ) ;_ lambda
    ) ;_ function
    lst
  ) ;_ mapcar
) ;_ defun
Title: Re: Replace nth member of a list
Post by: CAB on December 27, 2006, 10:50:53 AM
Now that is a pretty solution. :)
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 27, 2006, 11:00:45 AM
Now that is a pretty solution. :)
:)
Code: [Select]
(defun replase_1 (lst i itm)
  ;;(replase_1 '(0 1 2 3 4 5 6) 3 "A")
  ;; =>
  ;; '(0 1 2 "A" 4 5 6)
  (if lst
    (if (> i 0)
      (cons (car lst) (replase_1 (cdr lst) (1- i) itm))
      (cons itm (cdr lst))
    ) ;_ if
  ) ;_ if
)
Title: Re: Replace nth member of a list
Post by: John Kaul (Se7en) on December 27, 2006, 11:07:22 AM
Darn it!! Beaten by the recursion master!!

I swear i typed this up just now without seeing Evgeniy's procedure.

Code: [Select]
(defun nth-replace ( pos new-item lst )
  ;; Nth-Replacer
  ;;
  ;; This procedure will itterate thru an
  ;; entire list to replace the nth item
  ;; of that list.
  ;;
  ;; (nth-replace 2 3 '(1 2 4 4))
  ;; (1 2 3 4)
  ;;
  ;; By: John (Se7en) K
  ;;     12.27.06
  ;;
  (if (null lst)
    nil
    (cons
      (if (eq pos 0) new-item (car lst))
      (nth-replace (1- pos) new-item (cdr lst)))) )
Title: Re: Replace nth member of a list
Post by: CAB on December 27, 2006, 11:20:36 AM
I was able to do this variation of Evgeniy's solution.
Code: [Select]
(defun replace (lst i itm)
  (setq i (1+ i))
  (mapcar
    '(lambda (x)
      (if (zerop (setq i (1- i))) itm x)
    )
    lst
  )
)

Alas, I feel like I am in the shadows of a Giant.
Title: Re: Replace nth member of a list
Post by: Mark on December 27, 2006, 11:34:05 AM
Alas, I feel like I am in the shadows of a Giant.

I feel like a total newbie. You guys are amazing!
Title: Re: Replace nth member of a list
Post by: John Kaul (Se7en) on December 27, 2006, 11:39:06 AM
I like to map out the procedures; its fun.

CAB,
Its funny, because yours, mine, and Evgeniy's are basically the same procedure ...Especially Your last one; Mine and yours are almost identical!
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 27, 2006, 11:41:41 AM
>CAB

You write excellent programs! I too find new ideas and algorithms in your programs!!!
Title: Re: Replace nth member of a list
Post by: CAB on December 27, 2006, 11:51:19 AM
Thanks fellas, I just feel a step or two slow at times.  :?
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 27, 2006, 11:54:57 AM
As alternative...
Code: [Select]
(defun replase_2 (lst i itm / c)
  ;;(replase_2 '(0 1 2 3 4 5 6) 3 "A")
  ;; =>
  ;; '(0 1 2 "A" 4 5 6)
  (setq c -1)
  (mapcar
    (function cdr)
    ((lambda (l)
       (subst
         (cons 0 itm)
         (assoc i l)
         l
       ) ;_ subst
     ) ;_ lambda
      (mapcar (function (lambda (x) (cons (setq c (1+ c)) x))) lst)
    )
  ) ;_ mapcar
) ;_ defun
Title: Topic of nths of a lists
Post by: terrycadd on December 27, 2006, 03:12:14 PM
On the topic of nths of lists, here is a function that I wrote a long while back. It’s a little bit lengthy, but since it works I never looked into modifying it. Has anyone written a shorter version that does the same thing?
Code: [Select]
;-------------------------------------------------------------------------------
; Move_nth - Moves the nth Num1# item value to the nth Num2# location in a list.
; Arguments: 3
;   Num1# = Nth number in list to move item value
;   Num2# = Nth number in list to move item value of nth Num1# into
;   OldList@ = List to move item values
; Returns: A list with nth item value moved.
;-------------------------------------------------------------------------------
(defun Move_nth (Num1# Num2# OldList@ / Cnt# Item NewList@ Num1Value Valid)
  (if (and (= (type Num1#) 'int)(= (type Num2#) 'int)(= (type OldList@) 'list))
    (setq Valid t)
  );if
  (if (and Valid (< Num1# (length OldList@))(< Num2# (length OldList@))
      (/= Num1# Num2#)
    );and
    (progn
      (setq Cnt# 0)
      (setq Num1Value (nth Num1# OldList@))
      (foreach Item OldList@
        (if (/= Cnt# Num1#)
          (if (< Num1# Num2#)
            (if (/= Cnt# Num2#)
              (if NewList@
                (setq NewList@ (append NewList@ (list Item)))
                (setq NewList@ (list Item))
              );if
              (progn
                (if NewList@
                  (setq NewList@ (append NewList@ (list Item)))
                  (setq NewList@ (list Item))
                );if
                (if NewList@
                  (setq NewList@ (append NewList@ (list Num1Value)))
                  (setq NewList@ (list Num1Value))
                );if
              );progn
            );if
            (if (/= Cnt# Num2#)
              (if NewList@
                (setq NewList@ (append NewList@ (list Item)))
                (setq NewList@ (list Item))
              );if
              (progn
                (if NewList@
                  (setq NewList@ (append NewList@ (list Num1Value)))
                  (setq NewList@ (list Num1Value))
                );if
                (if NewList@
                  (setq NewList@ (append NewList@ (list Item)))
                  (setq NewList@ (list Item))
                );if
              );progn
            );if
          );if
        );if
        (setq Cnt# (1+ Cnt#))
      );foreach
    );progn
    (setq NewList@ OldList@)
  );if
  NewList@
);defun Move_nth
Title: Re: Replace nth member of a list
Post by: CAB on December 27, 2006, 06:54:55 PM
How about a Remove the Nth item.

Code: [Select]
;;  CAB 12/27/2006
;;  (RemoveNth 3 '(0 1 2 3 4 5))
;;  (0 1 2 4 5)
(defun removeNth (i lst)
  (setq i (1+ i))
  (vl-remove-if '(lambda(x) (zerop (setq i (1- i)))) lst)
)
Title: Re: Replace nth member of a list
Post by: It's Alive! on December 27, 2006, 07:20:47 PM
how about
Code: [Select]
;;(RemoveNth 3 '(0 1 2 3 4 5))

(defun removeNth (i lst)
(if (< i (length lst))
  (vl-remove (nth i lst) lst)
  )
)

Dan

<KB: added code tags>

Thanks  :-)
Title: Re: Replace nth member of a list
Post by: CAB on December 27, 2006, 07:31:53 PM
try this
Code: [Select]
(RemoveNth 3 '(3 1 2 3 4 5))
Title: Re: Replace nth member of a list
Post by: Kerry on December 27, 2006, 07:42:04 PM
great thread guys ..
Title: Re: Replace nth member of a list
Post by: It's Alive! on December 27, 2006, 07:46:09 PM
try this
Code: [Select]
(RemoveNth 3 '(3 1 2 3 4 5))

hmm not good

(1 2 4 5)
Title: Re: Replace nth member of a list
Post by: It's Alive! on December 27, 2006, 08:27:31 PM
ok I think this is a little better

Code: [Select]
;;(replace "a" 3 '(0 1 2 3 4 5 6))
(defun replace (a n lst / e i nl)
 (setq nl '()
       i 0
 )
 (foreach e lst
  (if (= i n)
   (setq nl (cons a nl))
   (setq nl (cons e nl))
  )
  (setq i (1+ i))
 )
 (reverse nl)
)

and

Code: [Select]


;;(RemoveNth 3 '(3 0 1 2 3 4 5))
(defun removeNth (n lst / e i nl)
 (setq nl '()
       i 0
 )
 (foreach e lst
  (if (/= i n)
   (setq nl (cons e nl))
  )
  (setq i (1+ i))
 )
 (reverse nl)
)
Title: Re: Replace nth member of a list
Post by: CAB on December 27, 2006, 09:26:45 PM
Terry,
How about this?
Code: [Select]
;;  CAB 12/27/06
;;  move i1 to i2 in list
;; Does not catch i1 or i2 <0 or > (length lst)
(defun MoveNth (i1 i2 lst / idx)
  (setq idx -1)
  (mapcar '(lambda (x)
             (setq idx (1+ idx))
             (cond
                 ((= idx i2) (nth i1 lst))
                 ((and (> i1 i2) (or (< idx i2)(> idx i1))) x)
                 ((and (> i2 i1) (>= idx i2)) x)
                 ((and (> i2 i1) (>= idx i1)) (nth (1+ idx) lst))
                 ((> idx i2) (nth (1- idx) lst))
               (x)
             )
           )
          lst
  )
)
PS Does not catch index out of range, i1 or i2 < 0 or > (length lst)
Title: Re: Replace nth member of a list
Post by: terrycadd on December 28, 2006, 12:09:28 AM
Sweet! You reduced it to less than a third. Great code. Here is another one that I use for removing a bunch of items at a time. It’s not as lengthy as move_nth, but can be improved upon.
Code: [Select]
;-------------------------------------------------------------------------------
; Remove_nths - Removes the RemoveList@ of nths from a list.
; Arguments: 2
;   RemoveList@ = List of nths to remove
;   OldList@ = List to remove the list of nths from
; Returns: A list with the list of nths removed.
;-------------------------------------------------------------------------------
(defun Remove_nths (RemoveList@ OldList@ / Cnt# Item NewList@)
  (setq Cnt# 0)
  (foreach Item OldList@
    (if (not (member Cnt# RemoveList@))
      (setq NewList@ (append NewList@ (list Item)))
    );if
    (setq Cnt# (1+ Cnt#))
  );foreach
  NewList@
);defun Remove_nths
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 28, 2006, 04:01:03 AM
Code: [Select]

(defun Remove_nths (lst1 lst2)
  (if (and lst1 lst2)
    (if (zerop (car lst1))
      (Remove_nths (mapcar '1- (cdr lst1)) (cdr lst2))
      (cons (car lst2) (Remove_nths (mapcar '1- lst1) (cdr lst2)))
    ) ;_ if
    lst2
  ) ;_ if
) ;_ defun


Test:

Code: [Select]

(setq lst1 (vl-sort '(4 20 0) '<)  lst2 '("a" "b" "c" "d" "e" "f"))
(Remove_nths lst1 lst2)
 =>>
'("b" "c" "d" "f")

Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 28, 2006, 05:23:36 AM
Code: [Select]
(defun MoveNth (i1 i2 lst / MoveNth_1)
  (defun MoveNth_1 (i1 i2 lst x i)
    (cond
      ((or (and (> i i1) (> i i2)) (= i1 i2)) lst)
      ((= i i1) (MoveNth_1 i1 (1+ i2) (cdr lst) x (1+ i)))
      ((= i i2) (cons x (MoveNth_1 (1+ i1) i2 lst x (1+ i))))
      ((cons (car lst) (MoveNth_1 i1 i2 (cdr lst) x (1+ i))))
    ) ;_ cond
  ) ;_ defun
  (MoveNth_1 i1 i2 lst (nth i1 lst) 0)
) ;_ defun

Test:

Code: [Select]
(MoveNth 3 1  '("a" "b" "c" "d" "e" "f"));=> '("a" "d" "b" "c" "e" "f")
(MoveNth 1 3  '("a" "b" "c" "d" "e" "f"));=> '("a" "c" "d" "b" "e" "f")
(MoveNth 1 10 '("a" "b" "c" "d" "e" "f"));=> '("a" "c" "d" "e" "f" nil nil nil nil nil "b")
(MoveNth 10 1 '("a" "b" "c" "d" "e" "f"));=> '("a" nil "b" "c" "d" "e" "f" nil nil nil nil)
Title: Re: Replace nth member of a list
Post by: Kerry on December 28, 2006, 05:31:40 AM
nice lesson Evgeniy   !!
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 28, 2006, 06:02:00 AM
nice lesson Evgeniy   !!


Thanks!  :-)

Other variant...
Code: [Select]
(defun MoveNth (i1 i2 lst / MoveNth_1)
  (defun MoveNth_1 (i1 i2 lst x)
    (cond
      ((or (and (minusp i1) (minusp i2)) (= i1 i2)) lst)
      ((zerop i1) (MoveNth_1 (1- i1) i2 (cdr lst) x))
      ((zerop i2) (cons x (MoveNth_1 i1 (1- i2) lst x)))
      ((cons (car lst) (MoveNth_1 (1- i1) (1- i2) (cdr lst) x)))
    ) ;_ cond
  ) ;_ defun
  (MoveNth_1 i1 i2 lst (nth i1 lst))
) ;_ defun
Title: Topic of nths of lists
Post by: terrycadd on December 28, 2006, 10:55:53 AM
The versions of moventh presented so far return the same results if the arguments for the nths are possible in the list. My version returns the original list if the arguments for the nths are not possible. I’m not comfortable with adding nils in a list where you might be expecting some other type of data. That means you would have to always have to double check if the value you need is a real value.
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 28, 2006, 11:04:35 AM
The versions of moventh presented so far return the same results if the arguments for the nths are possible in the list. My version returns the original list if the arguments for the nths are not possible. I’m not comfortable with adding nils in a list where you might be expecting some other type of data. That means you would have to always have to double check if the value you need is a real value.

No problem...


Code: [Select]
(defun MoveNth (i1 i2 lst / MoveNth_1)
  (defun MoveNth_1 (i1 i2 lst x i)
    (cond
      ((and (> i i1) (> i i2)) lst)
      ((= i i1) (MoveNth_1 i1 (1+ i2) (cdr lst) x (1+ i)))
      ((= i i2) (cons x (MoveNth_1 (1+ i1) i2 lst x (1+ i))))
      ((cons (car lst) (MoveNth_1 i1 i2 (cdr lst) x (1+ i))))
    ) ;_ cond
  ) ;_ defun
  (if (and (/= i1 i2) (<= 0 i1 (length lst)) (<= 0 i2 (length lst)))
    (MoveNth_1 i1 i2 lst (nth i1 lst) 0)
    lst
  ) ;_ if
) ;_ defun

<edit: modified code>
Title: Re: Replace nth member of a list
Post by: CAB on December 28, 2006, 11:13:45 AM
As Evgeniy said, "No Problem" :)

Code: [Select]
;;  CAB 12/28/06
;;  move i1 to i2 in list
(defun MoveNth (i1 i2 lst / idx)
  (setq idx -1)
  (if (and (< -1 i1 (length lst)) (< -1 i2 (length lst)))
    (mapcar '(lambda (x)
               (setq idx (1+ idx))
               (cond
                 ((= idx i2) (nth i1 lst))
                 ((and (> i1 i2) (or (< idx i2)(> idx i1))) x)
                 ((and (> i2 i1) (>= idx i2)) x)
                 ((and (> i2 i1) (>= idx i1)) (nth (1+ idx) lst))
                 ((> idx i2) (nth (1- idx) lst))
                 (x)
               )
             )
      lst
    )
    lst ; remove this line if you want nil returned if out of range
  )
)

Code: [Select]
(defun c:test ()
  (print (MoveNth 3 1 '("a" "b" "c" "d" "e" "f")))
  (print (MoveNth 1 3 '("a" "b" "c" "d" "e" "f")))
  (print (MoveNth 1 10 '("a" "b" "c" "d" "e" "f")))
  (print (MoveNth 10 1 '("a" "b" "c" "d" "e" "f")))
  (princ)
)

("a" "d" "b" "c" "e" "f")
("a" "c" "d" "b" "e" "f")
("a" "b" "c" "d" "e" "f")
("a" "b" "c" "d" "e" "f")

<edit: modified code>
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 28, 2006, 11:16:57 AM
As Evgeniy said, "No Problem" :)

 :-D  :-D  :-D
Title: Re: Replace nth member of a list
Post by: terrycadd on December 28, 2006, 02:20:08 PM
You guys are the greatest. This is a fun thread.
The checks in the mail. (ha!)
Title: Re: Replace nth member of a list
Post by: Keith™ on December 28, 2006, 02:51:11 PM
You guys are the greatest. This is a fun thread.
The checks in the mail. (ha!)
You should come and visit more often .. there are lots of other threads very similar to this one.

Oh .. and lest I forget .. the ideas posted here helped me resolve my problem and with a minor tweak to the combined code of CAB and Evgeniy I now have a working model that I can build upon. I appreciate the help guys .. seems like every now and then I just get a brain fart and can't seem to see the forest for all of the trees in the way.
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 28, 2006, 07:04:39 PM
You guys are the greatest. This is a fun thread.
The checks in the mail. (ha!)

My mail empty...  :(
Title: Re: Replace nth member of a list
Post by: terrycadd on December 30, 2006, 12:51:16 AM
I know this is not a contest, however concerning the speed of functions we can all benefit by the contributions of other programmers. Here are the results of the functions using my included c:TestNths function that determines the speed of the functions relating to the original title of this thread.
Code: [Select]
Command: TestNths
swapnth: 21543086
replase: 21544061
nth-replace: 21544324
replace: 21544728
replase_2: 21544986
Finished: 21545784
Lowest number for results is the fastest:
swapnth = 975
replase = 263
nth-replace = 404
replace = 258
replase_2 = 798

Command: TestNths
swapnth: 21550007
replase: 21550971
nth-replace: 21551233
replace: 21551635
replase_2: 21551893
Finished: 21552689
Lowest number for results is the fastest:
swapnth = 964
replase = 262
nth-replace = 402
replace = 258
replase_2 = 796

Command: TestNths
swapnth: 21593135
replase: 21594107
nth-replace: 21594370
replace: 21594772
replase_2: 21595030
Finished: 21595830
Lowest number for results is the fastest:
swapnth = 972
replase = 263
nth-replace = 402
replace = 258
replase_2 = 800

Command: TestNths
swapnth: 22000948
replase: 22001915
nth-replace: 22002177
replace: 22002579
replase_2: 22002837
Finished: 22003632
Lowest number for results is the fastest:
swapnth = 967
replase = 262
nth-replace = 402
replace = 258
replase_2 = 795
I ran this test several times and it returned similar results.
The “replace” function by Danielm103 was slightly faster than the “replase” function by Evgeniy.
Code: [Select]
(defun c:TestNths (/ Cnt# t1 t2 t3 t4 t5 t6)
  (if (not *List@);Create a 1000 item global *List@
    (repeat 10
      (setq Cnt# 48);Start at (ascii "0") (chr 48)
      (repeat 100
        (setq *List@ (append *List@ (list (chr Cnt#))))
        (setq Cnt# (1+ Cnt#))
      );repeat
    );repeat
  );if
  (princ "\nswapnth: ")
  (princ (setq t1 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
  (repeat 1000 (swapnth *List@ 998 "NEW"))
  (princ "\nreplase: ")
  (princ (setq t2 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
  (repeat 1000 (replase *List@ 998 "NEW"))
  (princ "\nnth-replace: ")
  (princ (setq t3 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
  (repeat 1000 (nth-replace 998 "NEW" *List@))
  (princ "\nreplace: ")
  (princ (setq t4 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
  (repeat 1000 (replace *List@ 998 "NEW"))
  (princ "\nreplase_2: ")
  (princ (setq t5 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
  (repeat 1000 (replase_2 *List@ 998 "NEW"))
  (princ "\nFinished: ")
  (princ (setq t6 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
  (princ "\nLowest number for results is the fastest:")
  (princ "\nswapnth = ")    (princ (- t2 t1))
  (princ "\nreplase = ")    (princ (- t3 t2))
  (princ "\nnth-replace = ")(princ (- t4 t3))
  (princ "\nreplace = ")    (princ (- t5 t4))
  (princ "\nreplase_2 = ")  (princ (- t6 t5))
  (princ)
);defun c:TestNths
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 30, 2006, 03:56:15 AM
I have obtained similar data, without compilation...
Compare speed after compilation!
AutoCad 2004

Checked this code:
Code: [Select]
;; CAB 11/15/2006
;;  replace nth item in list
(defun swapnth (lst i1 itm / tmp nlst)
  (while (and (> i1 0)
              (< (length (setq nlst (cons (car lst) nlst))) i1))
    (setq lst (cdr lst))
    )
  (setq nlst (cons itm nlst)
        lst (cddr lst))
  (while (or (setq tmp (car lst)) lst)
    (setq nlst (cons tmp nlst)
          lst (cdr lst))
    )
  (reverse nlst)
)
(defun replase (lst i itm)
  ;;(replase '(0 1 2 3 4 5 6) 3 "A")
  ;; =>
  ;; '(0 1 2 "A" 4 5 6)
  (mapcar
    (function
      (lambda (x)
        (if (zerop i)
          (progn (setq i (1- i)) itm)
          (progn (setq i (1- i)) x)
        ) ;_ if
      ) ;_ lambda
    ) ;_ function
    lst
  ) ;_ mapcar
) ;_ defun
(defun replase_1 (lst i itm)
  ;;(replase_1 '(0 1 2 3 4 5 6) 3 "A")
  ;; =>
  ;; '(0 1 2 "A" 4 5 6)
  (if lst
    (if (> i 0)
      (cons (car lst) (replase_1 (cdr lst) (1- i) itm))
      (cons itm (cdr lst))
    ) ;_ if
  ) ;_ if
)
(defun nth-replace ( pos new-item lst )
  ;; Nth-Replacer
  ;;
  ;; This procedure will itterate thru an
  ;; entire list to replace the nth item
  ;; of that list.
  ;;
  ;; (nth-replace 2 3 '(1 2 4 4))
  ;; (1 2 3 4)
  ;;
  ;; By: John (Se7en) K
  ;;     12.27.06
  ;;
  (if (null lst)
    nil
    (cons
      (if (eq pos 0) new-item (car lst))
      (nth-replace (1- pos) new-item (cdr lst)))) )
(defun replace (lst i itm)
  (setq i (1+ i))
  (mapcar
    '(lambda (x)
      (if (zerop (setq i (1- i))) itm x)
    )
    lst
  )
)
(defun replase_2 (lst i itm / c)
  ;;(replase_2 '(0 1 2 3 4 5 6) 3 "A")
  ;; =>
  ;; '(0 1 2 "A" 4 5 6)
  (setq c -1)
  (mapcar
    (function cdr)
    ((lambda (l)
       (subst
         (cons 0 itm)
         (assoc i l)
         l
       ) ;_ subst
     ) ;_ lambda
      (mapcar (function (lambda (x) (cons (setq c (1+ c)) x))) lst)
    )
  ) ;_ mapcar
) ;_ defun
(defun c:TestNths (/ Cnt# t1 t2 t3 t4 t5 t6)
  (if (not *List@);Create a 1000 item global *List@
    (repeat 10
      (setq Cnt# 48);Start at (ascii "0") (chr 48)
      (repeat 100
        (setq *List@ (append *List@ (list (chr Cnt#))))
        (setq Cnt# (1+ Cnt#))
      );repeat
    );repeat
  );if
  (princ "\nswapnth: ")
  (princ (setq t1 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
  (repeat 1000 (swapnth *List@ 998 "NEW"))
  (princ "\nreplase: ")
  (princ (setq t2 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
  (repeat 1000 (replase *List@ 998 "NEW"))
  (princ "\nnth-replace: ")
  (princ (setq t3 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
  (repeat 1000 (nth-replace 998 "NEW" *List@))
  (princ "\nreplace: ")
  (princ (setq t4 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
  (repeat 1000 (replace *List@ 998 "NEW"))
  (princ "\nreplase_1: ")
  (princ (setq t5 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
  (repeat 1000 (replase_1 *List@ 998 "NEW"))
  (princ "\nreplase_2: ")
  (princ (setq t6 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
  (repeat 1000 (replase_2 *List@ 998 "NEW"))
  (princ "\nFinished: ")
  (princ (setq t7 (atoi (substr (rtos (getvar "cdate") 2 8) 10))))(princ)
  (princ "\nLowest number for results is the fastest:")
  (princ "\nswapnth = ")    (princ (- t2 t1))
  (princ "\nreplase = ")    (princ (- t3 t2))
  (princ "\nnth-replace = ")(princ (- t4 t3))
  (princ "\nreplace = ")    (princ (- t5 t4))
  (princ "\nreplase_1 = ")  (princ (- t6 t5))
  (princ "\nreplase_2 = ")  (princ (- t7 t6))
  (princ)
);defun c:TestNths

Results after compilation:
Code: [Select]
Command:
Command: (LOAD "D:/replase.VLX") nil

Command: 'VLIDE
Command:
Command: TestNths
swapnth: 11341072
replase: 11341546
nth-replace: 11341621
replace: 11341769
replase_1: 11341891
replase_2: 11342038
Finished: 11342197
Lowest number for results is the fastest:
swapnth = 474
replase = 75
nth-replace = 148
replace = 122
replase_1 = 147
replase_2 = 159

Command:
Command: TestNths
swapnth: 11344692
replase: 11345168
nth-replace: 11345246
replace: 11345398
replase_1: 11345538
replase_2: 11345685
Finished: 11345851
Lowest number for results is the fastest:
swapnth = 476
replase = 78
nth-replace = 152
replace = 140
replase_1 = 147
replase_2 = 166

Command:
Command: TestNths
swapnth: 11350462
replase: 11350933
nth-replace: 11351009
replace: 11351155
replase_1: 11351276
replase_2: 11351423
Finished: 11351581
Lowest number for results is the fastest:
swapnth = 471
replase = 76
nth-replace = 146
replace = 121
replase_1 = 147
replase_2 = 158

Command:
Command: TestNths
swapnth: 11352441
replase: 11352913
nth-replace: 11352990
replace: 11353141
replase_1: 11353269
replase_2: 11353420
Finished: 11353582
Lowest number for results is the fastest:
swapnth = 472
replase = 77
nth-replace = 151
replace = 128
replase_1 = 151
replase_2 = 162

Command:
Command: TestNths
swapnth: 11354036
replase: 11354512
nth-replace: 11354588
replace: 11354739
replase_1: 11354871
replase_2: 11355021
Finished: 11355184
Lowest number for results is the fastest:
swapnth = 476
replase = 76
nth-replace = 151
replace = 132
replase_1 = 150
replase_2 = 163

Command:

Results without compilation:

Code: [Select]
Command:
Command: (LOAD "D:/replase.LSP") C:TESTNTHS

Command: TestNths

swapnth: 11533020
replase: 11533675
nth-replace: 11533829
replace: 11534141
replase_1: 11534278
replase_2: 11534565
Finished: 11534774
Lowest number for results is the fastest:
swapnth = 655
replase = 154
nth-replace = 312
replace = 137
replase_1 = 287
replase_2 = 209

Command:
Command: TestNths
swapnth: 11535754
replase: 11540417
nth-replace: 11540575
replace: 11540888
replase_1: 11541032
replase_2: 11541324
Finished: 11541538
Lowest number for results is the fastest:
swapnth = 4663
replase = 158
nth-replace = 313
replace = 144
replase_1 = 292
replase_2 = 214

Command:
Command: TestNths
swapnth: 11541771
replase: 11542436
nth-replace: 11542590
replace: 11542902
replase_1: 11543051
replase_2: 11543344
Finished: 11543559
Lowest number for results is the fastest:
swapnth = 665
replase = 154
nth-replace = 312
replace = 149
replase_1 = 293
replase_2 = 215

Command:
Command: TestNths
swapnth: 11543785
replase: 11544473
nth-replace: 11544641
replace: 11544969
replase_1: 11545104
replase_2: 11545421
Finished: 11545646
Lowest number for results is the fastest:
swapnth = 688
replase = 168
nth-replace = 328
replace = 135
replase_1 = 317
replase_2 = 225

Command:
Command: TestNths
swapnth: 11545831
replase: 11550502
nth-replace: 11550670
replace: 11550990
replase_1: 11551142
replase_2: 11551450
Finished: 11551669
Lowest number for results is the fastest:
swapnth = 4671
replase = 168
nth-replace = 320
replace = 152
replase_1 = 308
replase_2 = 219

Command:

PS. You use compilation?
 :-)
Title: Re: Replace nth member of a list
Post by: Kerry on December 30, 2006, 04:06:43 AM
Terry, Michael [ MP ] has published a Benchmark.lsp for comparing performance .

here you go : ...
http://www.theswamp.org/lilly_pond/mp/lisp/benchmark.txt?nossi=1
Code: [Select]
    ;;=================================================================
    ;;
    ;;  Example:
    ;;
    ;;      (BenchMark
    ;;         '(
    ;;              (1+ 1)
    ;;              (+ 1 1)
    ;;              (+ 1 1.0)
    ;;              (+ 1.0 1.0)
    ;;          )
    ;;      )
    ;;
    ;;=================================================================
    ;;
    ;;  Output:
    ;;
    ;;      Elapsed milliseconds / relative speed for 32768 iteration(s):
    ;;
    ;;          (1+ 1)..........1969 / 1.09 <fastest>
    ;;          (+ 1 1).........2078 / 1.03
    ;;          (+ 1 1.0).......2125 / 1.01
    ;;          (+ 1.0 1.0).....2140 / 1.00 <slowest>   
    ;;
    ;;=================================================================

;; .... >>>> etc

The version I use is modified so that the fastest has a 1 factor  and the slowest has a higher factor, related to the time
... so it looks like this :-
Code: [Select]
Benchmarking [M.P. @ theSwamp 2005] ..................
Elapsed milliseconds for 32768 iteration(s)/ relative Timing :

    (+ 1 1.0).......1735 / 1.0188 <slowest>
    (+ 1.0 1.0).....1735 / 1.0188
    (+ 1 1).........1719 / 1.0094
    (1+ 1)..........1703 / 1 <fastest>
Title: Re: Replace nth member of a list
Post by: CAB on December 30, 2006, 08:31:43 AM
This is what I got using MP Benchmark
Code: [Select]
Command: test
Elapsed milliseconds / relative speed for 16384 iteration(s):

    (REPLASE_1 (QUOTE (0 1 2 3 4 5 6)) 3...).....1973 / 1.15 <fastest>
    (SWAPNTH (QUOTE (0 1 2 3 4 5 6)) 3 "A")......2113 / 1.08
    (NTH-REPLACE 3 "a" (QUOTE (0 1 2 3 4...).....2193 / 1.04
    (REPLACE_CAB (QUOTE (0 1 2 3 4 5 6))...).....2193 / 1.04
    (REPLASE (QUOTE (0 1 2 3 4 5 6)) 3 "A")......2214 / 1.03
    (REPLACE_DA "a" 3 (QUOTE (0 1 2 3 4 ...).....2273 / 1.00 <slowest>

Command:
Command: test
Elapsed milliseconds / relative speed for 16384 iteration(s):

    (REPLASE_1 (QUOTE (0 1 2 3 4 5 6)) 3...).....1983 / 1.15 <fastest>
    (SWAPNTH (QUOTE (0 1 2 3 4 5 6)) 3 "A")......2123 / 1.08
    (REPLACE_CAB (QUOTE (0 1 2 3 4 5 6))...).....2203 / 1.04
    (NTH-REPLACE 3 "a" (QUOTE (0 1 2 3 4...).....2204 / 1.04
    (REPLASE (QUOTE (0 1 2 3 4 5 6)) 3 "A")......2223 / 1.03
    (REPLACE_DA "a" 3 (QUOTE (0 1 2 3 4 ...).....2283 / 1.00 <slowest>

Command:
Command: test
Elapsed milliseconds / relative speed for 16384 iteration(s):

    (REPLASE_1 (QUOTE (0 1 2 3 4 5 6)) 3...).....2003 / 1.14 <fastest>
    (SWAPNTH (QUOTE (0 1 2 3 4 5 6)) 3 "A")......2123 / 1.08
    (NTH-REPLACE 3 "a" (QUOTE (0 1 2 3 4...).....2204 / 1.04
    (REPLACE_CAB (QUOTE (0 1 2 3 4 5 6))...).....2223 / 1.03
    (REPLASE (QUOTE (0 1 2 3 4 5 6)) 3 "A")......2233 / 1.02
    (REPLACE_DA "a" 3 (QUOTE (0 1 2 3 4 ...).....2283 / 1.00 <slowest>
   
Code: [Select]
(defun c:test ()
  (BenchMark
    '((replace_Da "a" 3 '(0 1 2 3 4 5 6))
      (nth-replace 3 "a" '(0 1 2 3 4 5 6))
      (swapnth '(0 1 2 3 4 5 6) 3 "A")
      (replase '(0 1 2 3 4 5 6) 3 "A")
      (replace_CAB '(0 1 2 3 4 5 6) 3 "A")
      (replase_1 '(0 1 2 3 4 5 6) 3 "A")
     )
  )
)



Code: [Select]
;;  Danielm103
;;(replace "a" 3 '(0 1 2 3 4 5 6))
(defun replace_Da (a n lst / e i nl)
 (setq nl '()
       i 0
 )
 (foreach e lst
  (if (= i n)
   (setq nl (cons a nl))
   (setq nl (cons e nl))
  )
  (setq i (1+ i))
 )
 (reverse nl)
)


;; CAB 11/15/2006
;;  replace nth item in list
(defun swapnth (lst i1 itm / tmp nlst)
  (while (and (> i1 0)
              (< (length (setq nlst (cons (car lst) nlst))) i1))
    (setq lst (cdr lst))
    )
  (setq nlst (cons itm nlst)
        lst (cddr lst))
  (while (or (setq tmp (car lst)) lst)
    (setq nlst (cons tmp nlst)
          lst (cdr lst))
    )
  (reverse nlst)
)


;;  Evigeny
(defun replase (lst i itm)
  ;;(replase '(0 1 2 3 4 5 6) 3 "A")
  ;; =>
  ;; '(0 1 2 "A" 4 5 6)
  (mapcar
    (function
      (lambda (x)
        (if (zerop i)
          (progn (setq i (1- i)) itm)
          (progn (setq i (1- i)) x)
        )
      )
    )
    lst
  )
)


;; variation by CAB
(defun replace_CAB (lst i itm)
  (setq i (1+ i))
  (mapcar '(lambda (x) (if (zerop (setq i (1- i))) itm x)) lst)
)


;;  Evigeny
(defun replase_1 (lst i itm)
  ;;(replase_1 '(0 1 2 3 4 5 6) 3 "A")
  ;; =>
  ;; '(0 1 2 "A" 4 5 6)
  (if lst
    (if (> i 0)
      (cons (car lst) (replase_1 (cdr lst) (1- i) itm))
      (cons itm (cdr lst))
    ) ;_ if
  ) ;_ if
)

(defun nth-replace ( pos new-item lst )
  ;; Nth-Replacer
  ;;
  ;; This procedure will itterate thru an
  ;; entire list to replace the nth item
  ;; of that list.
  ;;
  ;; (nth-replace 2 3 '(1 2 4 4))
  ;; (1 2 3 4)
  ;;
  ;; By: John (Se7en) K
  ;;     12.27.06
  ;;
  (if (null lst)
    nil
    (cons
      (if (eq pos 0) new-item (car lst))
      (nth-replace (1- pos) new-item (cdr lst)))) )
Title: Re: Replace nth member of a list
Post by: CAB on December 30, 2006, 08:55:13 AM
Changing the test to this changed the results:
See Terry's code to create *List@
Code: [Select]
(defun c:test ()
  (BenchMark
    '((replace_Da "NEW" 998 *List@)
      (nth-replace 998 "NEW" *List@)
      (swapnth *List@ 998 "NEW")
      (replase *List@ 998 "NEW")
      (replace_CAB *List@ 998 "NEW")
      (replase_1 *List@ 998 "NEW")
     )
  )
)

Code: [Select]
Command: test
Elapsed milliseconds / relative speed for 512 iteration(s):

    (REPLACE_CAB *LIST@ 998 "NEW").....1011 / 3.90 <fastest>
    (REPLASE *LIST@ 998 "NEW").........1072 / 3.68
    (REPLASE_1 *LIST@ 998 "NEW").......1833 / 2.15
    (REPLACE_DA "NEW" 998 *LIST@)......1902 / 2.07
    (NTH-REPLACE 998 "NEW" *LIST@).....1952 / 2.02
    (SWAPNTH *LIST@ 998 "NEW").........3946 / 1.00 <slowest>

Command:
Command: test
Elapsed milliseconds / relative speed for 512 iteration(s):

    (REPLACE_CAB *LIST@ 998 "NEW").....1011 / 3.90 <fastest>
    (REPLASE *LIST@ 998 "NEW").........1072 / 3.68
    (REPLASE_1 *LIST@ 998 "NEW").......1833 / 2.15
    (REPLACE_DA "NEW" 998 *LIST@)......1923 / 2.05
    (NTH-REPLACE 998 "NEW" *LIST@).....2083 / 1.89
    (SWAPNTH *LIST@ 998 "NEW").........3946 / 1.00 <slowest>
   
   
And changing the test again to this:
Code: [Select]
(defun c:test ()
  (BenchMark
    '((replace_Da "NEW" 5 *List@)
      (nth-replace 998 "NEW" *List@)
      (swapnth *List@ 5 "NEW")
      (replase *List@ 5 "NEW")
      (replace_CAB *List@ 5 "NEW")
      (replase_1 *List@ 5 "NEW")
     )
  )
)
WOW - what do you suppose is going on here.. :)
Code: [Select]
Command: test
Elapsed milliseconds / relative speed for 8192 iteration(s):

    (REPLASE_1 *LIST@ 5 "NEW")........1011 / 30.91 <fastest>
    (REPLACE_CAB *LIST@ 5 "NEW").....16043 / 1.95
    (REPLASE *LIST@ 5 "NEW").........17195 / 1.82
    (SWAPNTH *LIST@ 5 "NEW").........22052 / 1.42
    (REPLACE_DA "NEW" 5 *LIST@)......30293 / 1.03
    (NTH-REPLACE 5 "NEW" *LIST@).....31245 / 1.00 <slowest>

Command:
Command: test
Elapsed milliseconds / relative speed for 8192 iteration(s):

    (REPLASE_1 *LIST@ 5 "NEW")........1021 / 30.65 <fastest>
    (REPLACE_CAB *LIST@ 5 "NEW").....16123 / 1.94
    (REPLASE *LIST@ 5 "NEW").........17335 / 1.81
    (SWAPNTH *LIST@ 5 "NEW").........22082 / 1.42
    (REPLACE_DA "NEW" 5 *LIST@)......30504 / 1.03
    (NTH-REPLACE 5 "NEW" *LIST@).....31295 / 1.00 <slowest>


<edit, added the second test routine...>
Title: Re: Replace nth member of a list
Post by: It's Alive! on December 30, 2006, 12:02:07 PM
Evigeny’s replase_1 Rocks!!!

Good job  Evigeny :-D
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 30, 2006, 01:10:14 PM
>CAB
You used compilation in this testing?

Title: Re: Replace nth member of a list
Post by: CAB on December 30, 2006, 06:18:47 PM
No, sorry, I was too lazy. :oops:

Oh, I forgot to include the second test routine, see the post above, I added it.

In your recursive routine, it stops when the replacement is made so in that test
it stopped after 6 iterations.
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 31, 2006, 05:08:49 AM
In your recursive routine, it stops when the replacement is made so in that test
it stopped after 6 iterations.

All works for me correctly...
Probably, I have not understood your question :(
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 31, 2006, 05:21:16 AM
No, sorry, I was too lazy. :oops:

I have made it.  :)
Very interesting results have turned out!  :-)

It is the program of testing:
Code: [Select]
(defun c:test (/ *LIST@ CNT# I)
  (if (not *List@)
    (repeat 10
      (setq Cnt# 48)
      (repeat 100
        (setq *List@ (append *List@ (list (chr Cnt#))))
        (setq Cnt# (1+ Cnt#))
      ) ;repeat
    ) ;repeat
  ) ;_  if
  (setq i 99)
  (repeat 10
    (princ (strcat "\n\nI = " (itoa i) "\n"))
    (BenchMark
      '((replace_Da "A" i *List@)
        (nth-replace i "A" *List@)
        (swapnth *List@ i "A")
        (replase *List@ i "A")
        (replace_CAB *List@ i "A")
        (replase_1 *List@ i "A")
        (replase_2 *List@ i "A")
       )
    ) ;_  BenchMark
    (setq i (+ i 100))
  ) ;_  repeat
) ;_  defun

It is a code, for testing:
Code: [Select]
;;  test.lsp
(defun replace_Da (a n lst / e i nl)
  ;;  Danielm103
  (setq nl '()
        i  0
  ) ;_  setq
  (foreach e lst
    (if (= i n)
      (setq nl (cons a nl))
      (setq nl (cons e nl))
    ) ;_  if
    (setq i (1+ i))
  ) ;_  foreach
  (reverse nl)
) ;_  defun
(defun swapnth (lst i1 itm / tmp nlst)
;; CAB 11/15/2006
;;  replace nth item in list
  (while (and (> i1 0)
              (< (length (setq nlst (cons (car lst) nlst))) i1)
         ) ;_  and
    (setq lst (cdr lst))
  ) ;_  while
  (setq nlst (cons itm nlst)
        lst  (cddr lst)
  ) ;_  setq
  (while (or (setq tmp (car lst)) lst)
    (setq nlst (cons tmp nlst)
          lst  (cdr lst)
    ) ;_  setq
  ) ;_  while
  (reverse nlst)
) ;_  defun
(defun replase (lst i itm)
  ;; ElpanovEvgeniy
  (mapcar
    (function
      (lambda (x)
        (if (zerop i)
          (progn (setq i (1- i)) itm)
          (progn (setq i (1- i)) x)
        ) ;_  if
      ) ;_  lambda
    ) ;_  function
    lst
  ) ;_  mapcar
) ;_  defun
(defun replace_CAB (lst i itm)
;; variation by CAB
  (setq i (1+ i))
  (mapcar '(lambda (x)
             (if (zerop (setq i (1- i)))
               itm
               x
             ) ;_  if
           ) ;_  lambda
          lst
  ) ;_  mapcar
) ;_  defun
(defun replase_1 (lst i itm)
  ;; ElpanovEvgeniy
  (if lst
    (if (> i 0)
      (cons (car lst) (replase_1 (cdr lst) (1- i) itm))
      (cons itm (cdr lst))
    ) ;_ if
  ) ;_ if
) ;_  defun
(defun nth-replace (pos new-item lst)
  ;; By: John (Se7en) K 12.27.06
  (if (null lst)
    nil
    (cons
      (if (eq pos 0)
        new-item
        (car lst)
      ) ;_  if
      (nth-replace (1- pos) new-item (cdr lst))
    ) ;_  cons
  ) ;_  if
) ;_  defun
(defun replase_2 (lst i itm / c)
  ;; ElpanovEvgeniy
  (setq c -1)
  (mapcar
    (function cdr)
    ((lambda (l)
       (subst
         (cons 0 itm)
         (assoc i l)
         l
       ) ;_ subst
     ) ;_ lambda
      (mapcar (function (lambda (x) (cons (setq c (1+ c)) x))) lst)
    )
  ) ;_ mapcar
)

These are results without compilation:
Code: [Select]
Command:
Command: (LOAD "E:/test.LSP") C:TEST

Command: test


I = 99
Elapsed milliseconds / relative speed for 4096 iteration(s):

    (REPLASE_1 *LIST@ I "A")........1437 / 9.99 <fastest>
    (REPLACE_CAB *LIST@ I "A")......6625 / 2.17
    (REPLASE *LIST@ I "A")..........7187 / 2.00
    (REPLASE_2 *LIST@ I "A").......10265 / 1.40
    (SWAPNTH *LIST@ I "A").........10313 / 1.39
    (NTH-REPLACE I "A" *LIST@).....13812 / 1.04
    (REPLACE_DA "A" I *LIST@)......14359 / 1.00 <slowest>


I = 199
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLASE_1 *LIST@ I "A").......1328 / 5.33 <fastest>
    (REPLACE_CAB *LIST@ I "A").....3234 / 2.19
    (REPLASE *LIST@ I "A").........3656 / 1.94
    (REPLASE_2 *LIST@ I "A").......5141 / 1.38
    (SWAPNTH *LIST@ I "A").........5766 / 1.23
    (NTH-REPLACE I "A" *LIST@).....6906 / 1.02
    (REPLACE_DA "A" I *LIST@)......7078 / 1.00 <slowest>


I = 299
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLASE_1 *LIST@ I "A").......1016 / 3.41 <fastest>
    (REPLACE_CAB *LIST@ I "A").....1672 / 2.07
    (REPLASE *LIST@ I "A").........1828 / 1.90
    (REPLASE_2 *LIST@ I "A").......2531 / 1.37
    (SWAPNTH *LIST@ I "A").........3156 / 1.10
    (NTH-REPLACE I "A" *LIST@).....3391 / 1.02
    (REPLACE_DA "A" I *LIST@)......3469 / 1.00 <slowest>


I = 399
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLASE_1 *LIST@ I "A").......1297 / 2.70 <fastest>
    (REPLACE_CAB *LIST@ I "A").....1672 / 2.09
    (REPLASE *LIST@ I "A").........1781 / 1.97
    (REPLASE_2 *LIST@ I "A").......2594 / 1.35
    (NTH-REPLACE I "A" *LIST@).....3484 / 1.00
    (REPLACE_DA "A" I *LIST@)......3500 / 1.00
    (SWAPNTH *LIST@ I "A").........3500 / 1.00 <slowest>


I = 499
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLACE_CAB *LIST@ I "A").....1578 / 2.53 <fastest>
    (REPLASE_1 *LIST@ I "A").......1657 / 2.40
    (REPLASE *LIST@ I "A").........1813 / 2.20
    (REPLASE_2 *LIST@ I "A").......2610 / 1.53
    (NTH-REPLACE I "A" *LIST@).....3422 / 1.16
    (REPLACE_DA "A" I *LIST@)......3547 / 1.12
    (SWAPNTH *LIST@ I "A").........3985 / 1.00 <slowest>


I = 599
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLACE_CAB *LIST@ I "A").....1671 / 2.58 <fastest>
    (REPLASE *LIST@ I "A").........1829 / 2.36
    (REPLASE_1 *LIST@ I "A").......1969 / 2.19
    (REPLASE_2 *LIST@ I "A").......2532 / 1.70
    (REPLACE_DA "A" I *LIST@)......3468 / 1.24
    (NTH-REPLACE I "A" *LIST@).....3516 / 1.23
    (SWAPNTH *LIST@ I "A").........4313 / 1.00 <slowest>


I = 699
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLACE_CAB *LIST@ I "A").....1641 / 3.06 <fastest>
    (REPLASE *LIST@ I "A").........1828 / 2.74
    (REPLASE_1 *LIST@ I "A").......2281 / 2.20
    (REPLASE_2 *LIST@ I "A").......2563 / 1.96
    (NTH-REPLACE I "A" *LIST@).....3390 / 1.48
    (REPLACE_DA "A" I *LIST@)......3594 / 1.40
    (SWAPNTH *LIST@ I "A").........5016 / 1.00 <slowest>


I = 799
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLACE_CAB *LIST@ I "A").....1593 / 3.58 <fastest>
    (REPLASE *LIST@ I "A").........1829 / 3.12
    (REPLASE_2 *LIST@ I "A").......2609 / 2.19
    (REPLASE_1 *LIST@ I "A").......2625 / 2.17
    (NTH-REPLACE I "A" *LIST@).....3375 / 1.69
    (REPLACE_DA "A" I *LIST@)......3609 / 1.58
    (SWAPNTH *LIST@ I "A").........5703 / 1.00 <slowest>


I = 899
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLACE_CAB *LIST@ I "A").....1562 / 4.08 <fastest>
    (REPLASE *LIST@ I "A").........1828 / 3.49
    (REPLASE_2 *LIST@ I "A").......2610 / 2.44
    (REPLASE_1 *LIST@ I "A").......2938 / 2.17
    (REPLACE_DA "A" I *LIST@)......3328 / 1.92
    (NTH-REPLACE I "A" *LIST@).....3453 / 1.85
    (SWAPNTH *LIST@ I "A").........6375 / 1.00 <slowest>


I = 999
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLACE_CAB *LIST@ I "A").....1421 / 5.00 <fastest>
    (REPLASE *LIST@ I "A").........1813 / 3.92
    (REPLASE_2 *LIST@ I "A").......2547 / 2.79
    (REPLASE_1 *LIST@ I "A").......3313 / 2.15
    (REPLACE_DA "A" I *LIST@)......3454 / 2.06
    (NTH-REPLACE I "A" *LIST@).....3516 / 2.02
    (SWAPNTH *LIST@ I "A").........7110 / 1.00 <slowest>
1099

Command:

These are results after compilation:
Code: [Select]
Command:
Command: (LOAD "E:/test.VLX") nil

Command: test


I = 99
Elapsed milliseconds / relative speed for 8192 iteration(s):

    (REPLASE_1 *LIST@ I "A")........1719 / 8.83 <fastest>
    (REPLASE *LIST@ I "A")..........7094 / 2.14
    (SWAPNTH *LIST@ I "A")..........8985 / 1.69
    (REPLACE_DA "A" I *LIST@).......9375 / 1.62
    (REPLACE_CAB *LIST@ I "A").....10156 / 1.49
    (NTH-REPLACE I "A" *LIST@).....13047 / 1.16
    (REPLASE_2 *LIST@ I "A").......15172 / 1.00 <slowest>


I = 199
Elapsed milliseconds / relative speed for 4096 iteration(s):

    (REPLASE_1 *LIST@ I "A").......1500 / 5.11 <fastest>
    (REPLASE *LIST@ I "A").........3547 / 2.16
    (REPLACE_DA "A" I *LIST@)......4688 / 1.64
    (REPLACE_CAB *LIST@ I "A").....5110 / 1.50
    (SWAPNTH *LIST@ I "A").........5187 / 1.48
    (NTH-REPLACE I "A" *LIST@).....6500 / 1.18
    (REPLASE_2 *LIST@ I "A").......7672 / 1.00 <slowest>


I = 299
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLASE_1 *LIST@ I "A").......1046 / 3.65 <fastest>
    (REPLASE *LIST@ I "A").........1766 / 2.16
    (REPLACE_DA "A" I *LIST@)......2375 / 1.61
    (REPLACE_CAB *LIST@ I "A").....2516 / 1.52
    (SWAPNTH *LIST@ I "A").........3062 / 1.25
    (NTH-REPLACE I "A" *LIST@).....3250 / 1.17
    (REPLASE_2 *LIST@ I "A").......3813 / 1.00 <slowest>


I = 399
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLASE_1 *LIST@ I "A").......1360 / 2.80 <fastest>
    (REPLASE *LIST@ I "A").........1781 / 2.14
    (REPLACE_DA "A" I *LIST@)......2360 / 1.62
    (REPLACE_CAB *LIST@ I "A").....2547 / 1.50
    (NTH-REPLACE I "A" *LIST@).....3250 / 1.17
    (SWAPNTH *LIST@ I "A").........3672 / 1.04
    (REPLASE_2 *LIST@ I "A").......3813 / 1.00 <slowest>


I = 499
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLASE_1 *LIST@ I "A").......1688 / 2.60 <fastest>
    (REPLASE *LIST@ I "A").........1782 / 2.46
    (REPLACE_DA "A" I *LIST@)......2328 / 1.89
    (REPLACE_CAB *LIST@ I "A").....2547 / 1.72
    (NTH-REPLACE I "A" *LIST@).....3265 / 1.34
    (REPLASE_2 *LIST@ I "A").......3828 / 1.15
    (SWAPNTH *LIST@ I "A").........4390 / 1.00 <slowest>


I = 599
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLASE *LIST@ I "A").........1765 / 2.97 <fastest>
    (REPLASE_1 *LIST@ I "A").......2000 / 2.62
    (REPLACE_DA "A" I *LIST@)......2344 / 2.23
    (REPLACE_CAB *LIST@ I "A").....2563 / 2.04
    (NTH-REPLACE I "A" *LIST@).....3265 / 1.60
    (REPLASE_2 *LIST@ I "A").......3828 / 1.37
    (SWAPNTH *LIST@ I "A").........5234 / 1.00 <slowest>


I = 699
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLASE *LIST@ I "A").........1766 / 3.53 <fastest>
    (REPLASE_1 *LIST@ I "A").......2297 / 2.71
    (REPLACE_DA "A" I *LIST@)......2344 / 2.66
    (REPLACE_CAB *LIST@ I "A").....2547 / 2.45
    (NTH-REPLACE I "A" *LIST@).....3266 / 1.91
    (REPLASE_2 *LIST@ I "A").......3828 / 1.63
    (SWAPNTH *LIST@ I "A").........6235 / 1.00 <slowest>


I = 799
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLASE *LIST@ I "A").........1781 / 4.13 <fastest>
    (REPLACE_DA "A" I *LIST@)......2344 / 3.14
    (REPLACE_CAB *LIST@ I "A").....2563 / 2.87
    (REPLASE_1 *LIST@ I "A").......2625 / 2.80
    (NTH-REPLACE I "A" *LIST@).....3250 / 2.26
    (REPLASE_2 *LIST@ I "A").......3859 / 1.91
    (SWAPNTH *LIST@ I "A").........7359 / 1.00 <slowest>


I = 899
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLASE *LIST@ I "A").........1766 / 4.88 <fastest>
    (REPLACE_DA "A" I *LIST@)......2344 / 3.67
    (REPLACE_CAB *LIST@ I "A").....2547 / 3.38
    (REPLASE_1 *LIST@ I "A").......2922 / 2.95
    (NTH-REPLACE I "A" *LIST@).....3265 / 2.64
    (REPLASE_2 *LIST@ I "A").......3844 / 2.24
    (SWAPNTH *LIST@ I "A").........8610 / 1.00 <slowest>


I = 999
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLASE *LIST@ I "A").........1781 / 5.61 <fastest>
    (REPLACE_DA "A" I *LIST@)......2343 / 4.26
    (REPLACE_CAB *LIST@ I "A").....2578 / 3.87
    (REPLASE_1 *LIST@ I "A").......3235 / 3.09
    (NTH-REPLACE I "A" *LIST@).....3250 / 3.07
    (REPLASE_2 *LIST@ I "A").......3860 / 2.59
    (SWAPNTH *LIST@ I "A").........9984 / 1.00 <slowest>
1099

Command:
Title: Re: Replace nth member of a list
Post by: CAB on December 31, 2006, 08:09:33 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.
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 31, 2006, 09:09:17 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...  :-)  :-)  :-)

Code: [Select]
(defun replace_CAB (lst i itm)
  ;; variation by CAB
  (setq i (1+ i))
  (mapcar '(lambda (x)
             (if (zerop (setq i (1- i)))
               itm
               x
             ) ;_  if
           ) ;_  lambda
          lst
  ) ;_  mapcar
) ;_  defun
(defun replace_CAB_optim (lst i itm)
  ;; variation by CAB
  (setq i (1+ i))
  (mapcar
    (function
      (lambda (x)
        (if (zerop (setq i (1- i)))
          itm
          x
        ) ;_  if
      ) ;_  lambda
    ) ;_  function
    lst
  ) ;_  mapcar
) ;_  defun

(defun c:test (/ *LIST@ CNT# I)
  (if (not *List@)
    (repeat 10
      (setq Cnt# 48)
      (repeat 100
        (setq *List@ (append *List@ (list (chr Cnt#))))
        (setq Cnt# (1+ Cnt#))
      ) ;repeat
    ) ;repeat
  ) ;_  if
  (setq i 99)
  (repeat 10
    (princ (strcat "\n\nI = " (itoa i) "\n"))
    (BenchMark
      '((replace_CAB *List@ i "A")
        (replace_CAB_optim *List@ i "A")
       )
    ) ;_  BenchMark
    (setq i (+ i 100))
  ) ;_  repeat
) ;_  defun

not compiled

Code: [Select]
Command: (LOAD "E:/test_1.LSP") C:TEST

Command: test


I = 99
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLACE_CAB *LIST@ I "A")...........1453 / 1.03 <fastest>
    (REPLACE_CAB_OPTIM *LIST@ I "A").....1500 / 1.00 <slowest>


I = 199
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLACE_CAB *LIST@ I "A")...........1407 / 1.07 <fastest>
    (REPLACE_CAB_OPTIM *LIST@ I "A").....1500 / 1.00 <slowest>


I = 299
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLACE_CAB *LIST@ I "A")...........1422 / 1.02 <fastest>
    (REPLACE_CAB_OPTIM *LIST@ I "A").....1454 / 1.00 <slowest>


I = 399
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLACE_CAB *LIST@ I "A")...........1406 / 1.04 <fastest>
    (REPLACE_CAB_OPTIM *LIST@ I "A").....1469 / 1.00 <slowest>


I = 499
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLACE_CAB *LIST@ I "A")...........1422 / 1.03 <fastest>
    (REPLACE_CAB_OPTIM *LIST@ I "A").....1469 / 1.00 <slowest>


I = 599
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLACE_CAB *LIST@ I "A")...........1422 / 1.03 <fastest>
    (REPLACE_CAB_OPTIM *LIST@ I "A").....1469 / 1.00 <slowest>


I = 699
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLACE_CAB *LIST@ I "A")...........1407 / 1.06 <fastest>
    (REPLACE_CAB_OPTIM *LIST@ I "A").....1485 / 1.00 <slowest>


I = 799
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLACE_CAB *LIST@ I "A")...........1406 / 1.06 <fastest>
    (REPLACE_CAB_OPTIM *LIST@ I "A").....1484 / 1.00 <slowest>


I = 899
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLACE_CAB *LIST@ I "A")...........1422 / 1.04 <fastest>
    (REPLACE_CAB_OPTIM *LIST@ I "A").....1485 / 1.00 <slowest>


I = 999
Elapsed milliseconds / relative speed for 1024 iteration(s):

    (REPLACE_CAB *LIST@ I "A")...........1422 / 1.02 <fastest>
    (REPLACE_CAB_OPTIM *LIST@ I "A").....1453 / 1.00 <slowest>
1099

Command:

after compiled

Code: [Select]
Command:
Command: (LOAD "E:/test_1.VLX") nil

Command: test


I = 99
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLACE_CAB_OPTIM *LIST@ I "A").....1719 / 1.47 <fastest>
    (REPLACE_CAB *LIST@ I "A")...........2531 / 1.00 <slowest>


I = 199
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLACE_CAB_OPTIM *LIST@ I "A").....1734 / 1.48 <fastest>
    (REPLACE_CAB *LIST@ I "A")...........2563 / 1.00 <slowest>


I = 299
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLACE_CAB_OPTIM *LIST@ I "A").....1672 / 1.51 <fastest>
    (REPLACE_CAB *LIST@ I "A")...........2531 / 1.00 <slowest>


I = 399
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLACE_CAB_OPTIM *LIST@ I "A").....1671 / 1.48 <fastest>
    (REPLACE_CAB *LIST@ I "A")...........2469 / 1.00 <slowest>


I = 499
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLACE_CAB_OPTIM *LIST@ I "A").....1671 / 1.52 <fastest>
    (REPLACE_CAB *LIST@ I "A")...........2532 / 1.00 <slowest>


I = 599
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLACE_CAB_OPTIM *LIST@ I "A").....1687 / 1.46 <fastest>
    (REPLACE_CAB *LIST@ I "A")...........2469 / 1.00 <slowest>


I = 699
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLACE_CAB_OPTIM *LIST@ I "A").....1672 / 1.52 <fastest>
    (REPLACE_CAB *LIST@ I "A")...........2546 / 1.00 <slowest>


I = 799
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLACE_CAB_OPTIM *LIST@ I "A").....1703 / 1.45 <fastest>
    (REPLACE_CAB *LIST@ I "A")...........2469 / 1.00 <slowest>


I = 899
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLACE_CAB_OPTIM *LIST@ I "A").....1688 / 1.49 <fastest>
    (REPLACE_CAB *LIST@ I "A")...........2516 / 1.00 <slowest>


I = 999
Elapsed milliseconds / relative speed for 2048 iteration(s):

    (REPLACE_CAB_OPTIM *LIST@ I "A").....1671 / 1.50 <fastest>
    (REPLACE_CAB *LIST@ I "A")...........2500 / 1.00 <slowest>
1099

Command:

 :-)
I congratulate on coming new year!
Let new year will bring a lot of pleasure and happiness!
Happy New Year!
 :-)
Title: Re: Replace nth member of a list
Post by: CAB on December 31, 2006, 01:21:25 PM
Thank you Sir. :)
And may you experience the same.

So, FUNCTION pays speed dividends. 8-)
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 31, 2006, 01:25:07 PM
I never saw, how you use FUNCTION...
Wished to show you a good example!
Title: Re: Replace nth member of a list
Post by: CAB on December 31, 2006, 01:58:19 PM
No, I was remarking how your use of FUNCTION made the routine faster when compiled.
I should have been more clear in my statement.
I never use the (Function but always use '(   Too lazy to type function.
It appears to only be a benefit when compiled though. Has that been your experance?

Title: Re: Replace nth member of a list
Post by: John Kaul (Se7en) on December 31, 2006, 02:36:56 PM
no
Title: Re: Replace nth member of a list
Post by: CAB on December 31, 2006, 02:40:18 PM
John, you have some examples where function preformed better?
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 31, 2006, 02:43:31 PM
Has that been your experience?

Yes, i read help for function...
I always use a full name (almost always)... :)
Title: Re: Replace nth member of a list
Post by: John Kaul (Se7en) on December 31, 2006, 05:24:26 PM
Not on me but ive done some testing on an application im creating for my company right now; Its about 850 lines of solid code and I tested several diff methods. However I supose we could whip one up. BUT I think you should be warned that just using one ``improvment'' in your test code inst going to matter much, you need to take other steps as well to ensure faster code.

Code: [Select]
(setq my-lst '(1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
               1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
               1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0))
(benchmark
 '(
    ( (lambda ( lst )
       (mapcar
        (function
         1+) lst))
      my-lst)
    ( (lambda ( lst )
       (mapcar '1+ lst))
      my-lst)
   )
 )

Elapsed milliseconds / relative speed for 8192 iteration(s):

    ((LAMBDA (LST) (MAPCAR (FUNCTION 1+)...).....1031 / 1.05 <fastest>
    ((LAMBDA (LST) (MAPCAR (QUOTE 1+) LS...).....1078 / 1.00 <slowest>

Command:  <Osnap off>
Command: (benchmark
(_>  '(
('(_>          ( (lambda ( lst )
('(((_>             (mapcar
('((((_>              (function
('(((((_>               1+) lst))
('((_>            my-lst)
('(_>          ( (lambda ( lst )
('(((_>             (mapcar '1+ lst))
('((_>            my-lst)
('(_>
('(_>
('(_>   )
(_>  )
Elapsed milliseconds / relative speed for 8192 iteration(s):

    ((LAMBDA (LST) (MAPCAR (FUNCTION 1+)...).....1015 / 1.06 <fastest>
    ((LAMBDA (LST) (MAPCAR (QUOTE 1+) LS...).....1078 / 1.00 <slowest>

Command: (benchmark
(_>  '(
('(_>     ( (lambda ( lst )
('(((_>        (mapcar
('((((_>         (function
('(((((_>          1+) lst))
('((_>       my-lst)
('(_>     ( (lambda ( lst )
('(((_>        (mapcar '1+ lst))
('((_>       my-lst)
('(_>    )
(_>  )
Elapsed milliseconds / relative speed for 8192 iteration(s):

    ((LAMBDA (LST) (MAPCAR (FUNCTION 1+)...).....1031 / 1.03 <fastest>
    ((LAMBDA (LST) (MAPCAR (QUOTE 1+) LS...).....1062 / 1.00 <slowest>
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy on December 31, 2006, 07:55:59 PM
If you do not compile the program it is possible to not use (function...

FUNCTION - helps to optimize the program at compilation.
In other cases from it there is not enough sense.

PS. I compile all the projects!
Title: Re: Replace nth member of a list
Post by: John Kaul (Se7en) on January 01, 2007, 12:05:26 AM
There is something really goofy going on here. ...I know you cant really test to see if a proced is faster by just a timed test, but this is just plain weird.

Code: [Select]
(progn
 (defun add-one-to-items  ( lst )
  (mapcar (function 1+) lst))

 (defun add-one-to-items--ver2 ( lst )
  (
   (lambda ( lst )
    (mapcar (function 1+) lst))
   lst))

 (defun add-one-to-items--ver3  ( lst )
  (mapcar '1+ lst))

 (defun add-one-to-items--ver4 ( lst )
  (
   (lambda ( lst )
    (mapcar '1+ lst))
   lst))

 (setq my-lst '(1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
                1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
                1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0))
 
 (benchmark
  '(
          (add-one-to-items my-lst)
          (add-one-to-items--ver2 my-lst)
          (add-one-to-items--ver3 my-lst)
          (add-one-to-items--ver4 my-lst)
    )
  )
 )

Elapsed milliseconds / relative speed for 16384 iteration(s):

    (ADD-ONE-TO-ITEMS--VER3 MY-LST).....1719 / 1.03 <fastest>
    (ADD-ONE-TO-ITEMS--VER4 MY-LST).....1719 / 1.03
    (ADD-ONE-TO-ITEMS--VER2 MY-LST).....1750 / 1.01
    (ADD-ONE-TO-ITEMS MY-LST)...........1765 / 1.00 <slowest>

Elapsed milliseconds / relative speed for 16384 iteration(s):

    (ADD-ONE-TO-ITEMS--VER2 MY-LST).....1750 / 1.01 <fastest>
    (ADD-ONE-TO-ITEMS--VER3 MY-LST).....1750 / 1.01
    (ADD-ONE-TO-ITEMS--VER4 MY-LST).....1750 / 1.01
    (ADD-ONE-TO-ITEMS MY-LST)...........1766 / 1.00 <slowest>
2 & 4 should be grouped together or 1 & 3 should... ?!

:?:
Title: Re: Replace nth member of a list
Post by: Keith™ on January 01, 2007, 01:23:41 AM
I don't mean to be a bug ... but could you guyz please use code tags around code segments ... I dunno why it bothers me so much .. but it does ...
Title: Re: Replace nth member of a list
Post by: irneb on December 20, 2012, 02:48:47 AM
Sorry to revive this ancient thread, just figured my version seems to outperform all the others in nearly every instance
Code - Auto/Visual Lisp: [Select]
  1. (defun IB:Replace-Nth  (Source Index Value / Result)
  2.   (if (< -1 Index (length Source))
  3.     (if (<= Index (/ (length Source) 2))
  4.       (progn (repeat (/ Index 4)
  5.                (setq Result (cons (cadddr Source)
  6.                                   (cons (caddr Source) (cons (cadr Source) (cons (car Source) Result))))
  7.                      Source (cddddr Source)))
  8.              (repeat (rem Index 4)
  9.                (setq Result (cons (car Source) Result)
  10.                      Source (cdr Source)))
  11.              (append (reverse Result) (cons Value (cdr Source))))
  12.       (reverse (IB:Replace-Nth (reverse Source) (- (length Source) Index) Value)))
  13.     Source))
And the results:
Code: [Select]
I = 50
Benchmarking ........ done for 8192 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(REPLASE_1 *LIST@ I A)                        8192      1170      1170     13.01
(IB:REPLACE-NTH *LIST@ I A)                   8192      1248      1248     12.20
(REPLACE_CAB *LIST@ I A)                      2048      1965      7860      1.94
(REPLASE *LIST@ I A)                          1024      1077      8616      1.77
(SWAPNTH *LIST@ I A)                          1024      1325     10600      1.44
(REPLASE_2 *LIST@ I A)                        1024      1732     13856      1.10
(REPLACE_DA A I *LIST@)                       1024      1842     14736      1.03
(NTH-REPLACE I A *LIST@)                      1024      1903     15224      1.00
--------------------------------------------------------------------------------

I = 150
Benchmarking ........ done for 4096 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A)                   4096      1170      1170      8.43
(REPLASE_1 *LIST@ I A)                        4096      1545      1545      6.38
(REPLACE_CAB *LIST@ I A)                      1024      1217      4868      2.03
(REPLASE *LIST@ I A)                          1024      1325      5300      1.86
(REPLASE_2 *LIST@ I A)                        1024      1762      7048      1.40
(SWAPNTH *LIST@ I A)                          1024      1888      7552      1.31
(REPLACE_DA A I *LIST@)                        512      1202      9616      1.03
(NTH-REPLACE I A *LIST@)                       512      1233      9864      1.00
--------------------------------------------------------------------------------

I = 250
Benchmarking ........ done for 4096 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A)                   4096      1653      1653      5.96
(REPLASE_1 *LIST@ I A)                        2048      1232      2464      4.00
(REPLACE_CAB *LIST@ I A)                      1024      1201      4804      2.05
(REPLASE *LIST@ I A)                          1024      1341      5364      1.84
(REPLASE_2 *LIST@ I A)                        1024      1733      6932      1.42
(SWAPNTH *LIST@ I A)                           512      1076      8608      1.14
(NTH-REPLACE I A *LIST@)                       512      1219      9752      1.01
(REPLACE_DA A I *LIST@)                        512      1232      9856      1.00
--------------------------------------------------------------------------------

I = 350
Benchmarking ........ done for 2048 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A)                   2048      1014      1014      4.99
(REPLASE_1 *LIST@ I A)                        2048      1700      1700      2.98
(REPLACE_CAB *LIST@ I A)                      1024      1217      2434      2.08
(REPLASE *LIST@ I A)                          1024      1357      2714      1.86
(REPLASE_2 *LIST@ I A)                        1024      1762      3524      1.44
(SWAPNTH *LIST@ I A)                           512      1186      4744      1.07
(REPLACE_DA A I *LIST@)                        512      1232      4928      1.03
(NTH-REPLACE I A *LIST@)                       512      1265      5060      1.00
--------------------------------------------------------------------------------

I = 450
Benchmarking ........ done for 2048 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A)                   2048      1184      1184      4.58
(REPLASE_1 *LIST@ I A)                        1024      1061      2122      2.56
(REPLACE_CAB *LIST@ I A)                      1024      1201      2402      2.26
(REPLASE *LIST@ I A)                          1024      1342      2684      2.02
(REPLASE_2 *LIST@ I A)                        1024      1794      3588      1.51
(REPLACE_DA A I *LIST@)                        512      1186      4744      1.14
(NTH-REPLACE I A *LIST@)                       512      1218      4872      1.11
(SWAPNTH *LIST@ I A)                           512      1357      5428      1.00
--------------------------------------------------------------------------------

I = 550
Benchmarking ........ done for 2048 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A)                   2048      1436      1436      4.26
(REPLACE_CAB *LIST@ I A)                      1024      1200      2400      2.55
(REPLASE_1 *LIST@ I A)                        1024      1264      2528      2.42
(REPLASE *LIST@ I A)                          1024      1340      2680      2.28
(REPLASE_2 *LIST@ I A)                        1024      1748      3496      1.75
(REPLACE_DA A I *LIST@)                        512      1233      4932      1.24
(NTH-REPLACE I A *LIST@)                       512      1248      4992      1.23
(SWAPNTH *LIST@ I A)                           512      1529      6116      1.00
--------------------------------------------------------------------------------

I = 650
Benchmarking ........ done for 2048 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A)                   2048      1653      1653      4.23
(REPLACE_CAB *LIST@ I A)                      1024      1232      2464      2.84
(REPLASE *LIST@ I A)                          1024      1310      2620      2.67
(REPLASE_1 *LIST@ I A)                        1024      1514      3028      2.31
(REPLASE_2 *LIST@ I A)                        1024      1764      3528      1.98
(REPLACE_DA A I *LIST@)                        512      1232      4928      1.42
(NTH-REPLACE I A *LIST@)                       512      1232      4928      1.42
(SWAPNTH *LIST@ I A)                           512      1747      6988      1.00
--------------------------------------------------------------------------------

I = 750
Benchmarking ........ done for 2048 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A)                   2048      1888      1888      4.17
(REPLACE_CAB *LIST@ I A)                      1024      1233      2466      3.19
(REPLASE *LIST@ I A)                          1024      1358      2716      2.90
(REPLASE_1 *LIST@ I A)                        1024      1717      3434      2.29
(REPLASE_2 *LIST@ I A)                        1024      1733      3466      2.27
(NTH-REPLACE I A *LIST@)                       512      1247      4988      1.58
(REPLACE_DA A I *LIST@)                        512      1248      4992      1.58
(SWAPNTH *LIST@ I A)                           512      1966      7864      1.00
--------------------------------------------------------------------------------

I = 850
Benchmarking ........ done for 2048 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A)                   2048      2029      2029      4.37
(REPLACE_CAB *LIST@ I A)                      1024      1201      2402      3.69
(REPLASE *LIST@ I A)                          1024      1310      2620      3.38
(REPLASE_2 *LIST@ I A)                        1024      1794      3588      2.47
(REPLASE_1 *LIST@ I A)                        1024      1965      3930      2.26
(REPLACE_DA A I *LIST@)                        512      1217      4868      1.82
(NTH-REPLACE I A *LIST@)                       512      1248      4992      1.78
(SWAPNTH *LIST@ I A)                           256      1108      8864      1.00
--------------------------------------------------------------------------------

I = 950
Benchmarking ........ done for 1024 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(IB:REPLACE-NTH *LIST@ I A)                   1024      1155      1155      4.32
(REPLACE_CAB *LIST@ I A)                      1024      1217      1217      4.10
(REPLASE *LIST@ I A)                          1024      1295      1295      3.85
(REPLASE_2 *LIST@ I A)                        1024      1778      1778      2.81
(REPLASE_1 *LIST@ I A)                         512      1092      2184      2.29
(NTH-REPLACE I A *LIST@)                       512      1183      2366      2.11
(REPLACE_DA A I *LIST@)                        512      1233      2466      2.02
(SWAPNTH *LIST@ I A)                           256      1248      4992      1.00
--------------------------------------------------------------------------------1050
_$
Title: Re: Replace nth member of a list
Post by: CAB on December 20, 2012, 01:15:46 PM
Where have you been for the past 6 years?  :evil:

Just kidding, looks like great performance in your test.
Title: Re: Replace nth member of a list
Post by: irneb on December 21, 2012, 12:34:23 AM
Yeah, wasn't on the Swamp that time. Was mostly hanging out on AutoDesk & Augi, when I had time (was working on this project):
(http://www.proscapegroup.com/images/pp/otci2.JPG)
So didn't have a lot of time to spare.

Anyhow, the principle of mine was actually from an algorithm I saw Elpanov Evgeniy use in another routine.
Title: Re: Replace nth member of a list
Post by: CAB on December 21, 2012, 07:54:03 AM
Anyhow, the principle of mine was actually from an algorithm I saw Elpanov Evgeniy use in another routine.

Ah yes, a lot of acorns here.  8-)
Title: Re: Replace nth member of a list
Post by: Lee Mac on December 21, 2012, 06:38:02 PM
@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.



ETA: After a quick test, it seems there is in fact a marginal performance benefit:

Code - Auto/Visual Lisp: [Select]
  1. (defun cons-it ( l / r )
  2.     (while l
  3.         (setq r (cons (cadddr l) (cons (caddr l) (cons (cadr l) (cons (car l) r))))
  4.               l (cddddr l)
  5.         )
  6.     )
  7.     (reverse r)
  8. )
  9.  
  10. (defun vl-list*-it ( l / r )
  11.     (while l
  12.         (setq r (vl-list* (cadddr l) (caddr l) (cadr l) (car l) r)
  13.               l (cddddr l)
  14.         )
  15.     )
  16.     (reverse r)
  17. )

Code - Auto/Visual Lisp: [Select]
  1. _$ (setq i 1)
  2. 1
  3. _$ (repeat 100 (setq l (cons i l) i (1+ i)))
  4. 101
  5. _$ (length l)
  6. 100
  7.  
  8. Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):
  9.  
  10.     (VL-LIST*-IT L).....1264 / 1.20 <fastest>
  11.     (CONS-IT L).........1513 / 1.00 <slowest>

Code - Auto/Visual Lisp: [Select]
  1. _$ (setq i 1)
  2. 1
  3. _$ (repeat 1000 (setq l (cons i l) i (1+ i)))
  4. 1001
  5. _$ (length l)
  6. 1000
  7.  
  8. Benchmarking ..............Elapsed milliseconds / relative speed for 2048 iteration(s):
  9.  
  10.     (VL-LIST*-IT L).....1107 / 1.25 <fastest>
  11.     (CONS-IT L).........1389 / 1.00 <slowest>
Title: Re: Replace nth member of a list
Post by: chlh_jd on December 24, 2012, 12:39:49 AM
All Nice codes  :-)
Here's my version
Code: [Select]
;; written by qj-chen
;; Edited by GSLS(SS)
(defun ch-lst (new i lst / j len fst mid)
  (if (/= (type i) (quote list))
    (cond
      ((not (listp lst))
       lst)
      ((minusp i)
       lst
      )
      ((> i (setq len (length lst)))
       lst
      )
      ((> i (/ len 2))
       (reverse (ch-lst new (1- (- len i)) (reverse lst)))
      )
      (t
       (append
(progn
   (setq fst nil)
   (repeat (rem i 4)
     (setq fst (cons (car lst) fst)
   lst (cdr lst)
     )
   )
   (repeat (/ i 4)
     (setq fst (cons (cadddr lst)
     (cons (caddr lst)
   (cons
     (cadr lst)
     (cons
       (car lst)
       fst
     )
   )
     )
       )
   lst (cddddr lst)
     )
   )
   (reverse fst)
)
(list new)
(cdr lst)
       )
      )
    )
    (progn
      (setq j (cadr i)
    i (car i)
      )
      (if j
(progn
  (setq mid (nth i lst))
  (setq mid (ch-lst new j mid))
  (ch-lst mid i lst)
)
(ch-lst new i lst)
      )
    )
  )
)
E.g.
Code: [Select]
(ch-lst 3 2 '(1 1 1 1 1 1 1)) --> '(1 1 3 1 1 1 1)
(ch-lst 3 '(2 2) '((1 1 1 1 1 1 ) (1 1 1 1 1 1 )  (1 1 1 1 1 1)  (1 1 1 1 1 1)  (1 1 1 1 1 1)  (1 1 1 1 1 1) ))
     -->' ((1 1 1 1 1 1)  (1 1 1 1 1 1)  (1 1 3 1 1 1)  (1 1 1 1 1 1)  (1 1 1 1 1 1)  (1 1 1 1 1 1))
Remove nth
Code: [Select]
(defun remove-nth (i lst / j len fst)
  (if (/= (type i) (quote list))
    (cond
      ((or (minusp i) (> i (1- (setq len (length lst)))))
       lst
      )     
      ((> i (/ len 2))
       (reverse (remove-nth (1- (- len i)) (reverse lst)))
      )
      (t
       (append
(progn
   (setq fst nil)
   (repeat (rem i 4)
     (setq fst (cons (car lst) fst)
   lst (cdr lst)
     )
   )
   (repeat (/ i 4)
     (setq fst (cons (cadddr lst)
     (cons (caddr lst)
   (cons
     (cadr lst)
     (cons
       (car lst)
       fst
     )
   )
     )
       )
   lst (cddddr lst)
     )
   )
   (reverse fst)
)
(cdr lst)
       )
      )
    )
    (progn
      (setq j (cadr i)
    i (car i)
      )
      (if j
(mapcar (function (lambda (x) (remove-nth j x))) (remove-nth i lst))
(remove-nth i lst)
      )
    )
  )
)
e.g.
Code: [Select]
(remove-nth 2 '(1 1 3 1 1 1 1)) --> '(1 1  1 1 1 1)
(remove-nth  '(2 2) ' ((1 1 1 1 1 1)  (1 1 1 1 1 1)  (1 1 3 1 1 1)  (1 1 1 1 1 1)  (1 1 1 1 1 1)  (1 1 1 1 1 1)))
     -->((1 1 1 1 1)  (1 1 1 1 1)  (1 1 1 1 1)  (1 1 1 1 1) (1 1 1 1 1))
Title: Re: Replace nth member of a list
Post by: chlh_jd 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 .
Title: Re: Replace nth member of a list
Post by: Lee Mac on December 24, 2012, 08:31:30 AM
Nice method , Lee .

Thanks  :-)
Title: Re: Replace nth member of a list
Post by: chlh_jd 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 !
Title: Re: Replace nth member of a list
Post by: ElpanovEvgeniy 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!  :-)
Title: Re: Replace nth member of a list
Post by: CADDOG on December 28, 2012, 01:02:49 PM
...for you consideration

LeeMac-replace3 (http://www.theswamp.org/index.php?topic=40076.msg456036#msg456036)

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:
Title: Re: Replace nth member of a list
Post by: Marc'Antonio Alessi 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))
    )
  )
)