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

0 Members and 1 Guest are viewing this topic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16867
  • Superior Stupidity at its best
Replace nth member of a list
« 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.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

CAB

  • Global Moderator
  • Seagull
  • Posts: 10395
Re: Replace nth member of a list
« Reply #1 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)
)
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.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1544
  • Moscow (Russia)
Re: Replace nth member of a list
« Reply #2 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
Stay home. Stay safe. Save lives.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10395
Re: Replace nth member of a list
« Reply #3 on: December 27, 2006, 10:50:53 AM »
Now that is a pretty solution. :)
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.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1544
  • Moscow (Russia)
Re: Replace nth member of a list
« Reply #4 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
)
Stay home. Stay safe. Save lives.

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9667
Re: Replace nth member of a list
« Reply #5 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)))) )
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10395
Re: Replace nth member of a list
« Reply #6 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.
« Last Edit: December 27, 2006, 11:22:49 AM by CAB »
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.

Mark

  • Custom Title
  • Seagull
  • Posts: 28708
Re: Replace nth member of a list
« Reply #7 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!
TheSwamp.org  (serving the CAD community since 2003)

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9667
Re: Replace nth member of a list
« Reply #8 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!
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1544
  • Moscow (Russia)
Re: Replace nth member of a list
« Reply #9 on: December 27, 2006, 11:41:41 AM »
>CAB

You write excellent programs! I too find new ideas and algorithms in your programs!!!
Stay home. Stay safe. Save lives.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10395
Re: Replace nth member of a list
« Reply #10 on: December 27, 2006, 11:51:19 AM »
Thanks fellas, I just feel a step or two slow at times.  :?
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.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1544
  • Moscow (Russia)
Re: Replace nth member of a list
« Reply #11 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
Stay home. Stay safe. Save lives.

terrycadd

  • Guest
Topic of nths of a lists
« Reply #12 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
« Last Edit: December 27, 2006, 03:29:37 PM by Terry Cadd »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10395
Re: Replace nth member of a list
« Reply #13 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)
)
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.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 7060
  • AKA Daniel
Re: Replace nth member of a list
« Reply #14 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  :-)
« Last Edit: December 27, 2006, 08:37:37 PM by Danielm103 »
Retired