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

0 Members and 1 Guest are viewing this topic.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10397
Re: Replace nth member of a list
« Reply #15 on: December 27, 2006, 07:31:53 PM »
try this
Code: [Select]
(RemoveNth 3 '(3 1 2 3 4 5))
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Replace nth member of a list
« Reply #16 on: December 27, 2006, 07:42:04 PM »
great thread guys ..
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 7160
  • AKA Daniel
Re: Replace nth member of a list
« Reply #17 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)
Retired

It's Alive!

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

CAB

  • Global Moderator
  • Seagull
  • Posts: 10397
Re: Replace nth member of a list
« Reply #19 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)
« Last Edit: December 28, 2006, 11:27:34 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.

terrycadd

  • Guest
Re: Replace nth member of a list
« Reply #20 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. Its 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
« Last Edit: December 28, 2006, 12:13:42 AM by Terry Cadd »

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1544
  • Moscow (Russia)
Re: Replace nth member of a list
« Reply #21 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")

Stay home. Stay safe. Save lives.

ElpanovEvgeniy

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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Replace nth member of a list
« Reply #23 on: December 28, 2006, 05:31:40 AM »
nice lesson Evgeniy   !!
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

ElpanovEvgeniy

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

terrycadd

  • Guest
Topic of nths of lists
« Reply #25 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. Im 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.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1544
  • Moscow (Russia)
Re: Replace nth member of a list
« Reply #26 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>
« Last Edit: December 28, 2006, 12:00:34 PM by ElpanovEvgeniy »
Stay home. Stay safe. Save lives.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10397
Re: Replace nth member of a list
« Reply #27 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>
« Last Edit: December 28, 2006, 11:25:30 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.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1544
  • Moscow (Russia)
Re: Replace nth member of a list
« Reply #28 on: December 28, 2006, 11:16:57 AM »
As Evgeniy said, "No Problem" :)

 :-D  :-D  :-D
Stay home. Stay safe. Save lives.

terrycadd

  • Guest
Re: Replace nth member of a list
« Reply #29 on: December 28, 2006, 02:20:08 PM »
You guys are the greatest. This is a fun thread.
The checks in the mail. (ha!)