Author Topic: RemoveNth ...  (Read 13776 times)

0 Members and 1 Guest are viewing this topic.

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: RemoveNth ...
« Reply #45 on: February 24, 2009, 03:05:47 PM »
I never saw this post, soory to ge t here so late.

Would this work?

Code: [Select]
(defun REMNTH (Ctrl Lst / NewList)
(if (and (numberp Ctrl)(>= Ctrl 0)(< Ctrl (length Lst))(listp Lst))
(setq NewList (vl-remove (nth Ctrl Lst) Lst))
)
)
« Last Edit: February 24, 2009, 03:11:05 PM by TimSpangler »
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright 2016

ronjonp

  • Needs a day job
  • Posts: 7524
Re: RemoveNth ...
« Reply #46 on: February 24, 2009, 03:08:48 PM »
I think vl-remove takes out all occurrences found in the list.

Yup...(vl-remove 1 '(1 1 1 2 2 3 3 4 5 6 7))  >>> (2 2 3 3 4 5 6 7)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: RemoveNth ...
« Reply #47 on: February 24, 2009, 03:12:23 PM »
Crap, you're right....... :lol:

Back to the dwg brd....
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright 2016

uncoolperson

  • Guest
Re: RemoveNth ...
« Reply #48 on: February 24, 2009, 07:31:42 PM »
dang how did i miss out on this bit of fun?

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2086
  • class keyThumper<T>:ILazy<T>
Re: RemoveNth ...
« Reply #49 on: February 24, 2009, 09:23:29 PM »
uhmmm ...
'cause you weren't a member when it happened ..perhaps ?


Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

uncoolperson

  • Guest
Re: RemoveNth ...
« Reply #50 on: February 24, 2009, 11:01:46 PM »
I should've atleast caught some of page 3

cjw

  • Guest
Re: RemoveNth ...
« Reply #51 on: February 26, 2009, 02:03:08 AM »
Quote
(defun FOO (X)
    (if   (eq N (setq I (1+ I)))
      (defun FOO (X) NIL)
    )
  )

It's funny!!!

First meet it

jxphklibin

  • Guest
Re: RemoveNth ...
« Reply #52 on: March 03, 2009, 02:38:43 AM »
not recursive
Code: [Select]
(defun remove-nth (l n / a)
 ;;(remove-nth l 20)
 (while (and l (> n 0))
  (setq a (cons (car l) a)
        l (cdr l)
        n (1- n)
  ) ;_  setq
 ) ;_  while
 (append (reverse a) (cdr l))
)

The above function questions exist, such as:
_$ (remove_nth -3 '("a" "b" "c" d e d f g h d d d))
("b" "c" D E D F G H D D D)
_$ (remove_nth 18 '("a" "b" "c" d e d f g h d d d))
("a" "b" "c" D E D F G H D D D)

When n is less than 0, the function of problem exist.

Here are my revised code:
Code: [Select]
;; modified by 小李子
(defun remove_nth (n l / a)
  (if (and l (>= n 0) (< n (length l)))
    (progn
      (while (> n 0)
(setq a (cons (car l) a)
      l (cdr l)
      n (1- n)
)
      )
      (setq l (append (reverse a) (cdr l)))
    )
  )
  l
)
« Last Edit: March 03, 2009, 03:20:51 AM by jxphklibin »

jxphklibin

  • Guest
Re: RemoveNth ...
« Reply #53 on: March 03, 2009, 02:51:29 AM »
Here is something similar I wrote a long time ago to be able to set or remove an atom from in a list.  It's a lot more "brute force" and the speed of it will depend on the position of the element that we are trying to remove. also, there is a lot of overhead from the double reverse of the Add function.

Code: [Select]
(Defun Add ( ATM LST )
 (Reverse (Cons ATM (Reverse LST)) )
)

(Defun SetNth ( IDX LST ITM / cnt return )
 (SetQ cnt 0 )
 (While (< cnt IDX)
  (SetQ return (Add (Car LST) return)
        LST    (Cdr LST)
        cnt    (1+ cnt)
  )
 )
 (SetQ return (Append (Add ITM return) (Cdr LST)) )


 return
)

(Defun RemoveNth ( IDX LST / cnt return )
 (SetQ cnt 0 )
 (While (< cnt IDX)
  (SetQ return (Add (Car LST) return)
        LST    (Cdr LST)
        cnt    (1+ cnt)
  )
 )
 (SetQ return (Append return (Cdr LST)) )

 return
)

It's very similar to ElpanovEvgeniy's solution, but he managed to get around the double reverse!

When n is less than 0 or greater than (length LST), the function of problem exist.such as:
_$ (remove-nth -3 '("a" "b" "c" d e d f g h d d d))
("b" "c" D E D F G H D D D)
_$ (remove-nth 18 '("a" "b" "c" d e d f g h d d d))
("a" "b" "c" D E D F G H D D D nil nil nil nil nil nil)

Here are my revised code:
Code: [Select]
;; 上面的函数有错误,所以修改后为:by 小李子
(Defun Remove-Nth-0 (IDX LST / cnt ret)
  (if (and LST (>= IDX 0) (< IDX (length LST)))
    (progn
      (SetQ cnt 0)
      (While (< cnt IDX)
(SetQ ret (Add (Car LST) ret)
      LST (Cdr LST)
      cnt (1+ cnt)
)
      )
      (SetQ ret (Append ret (Cdr LST)))
    )
    (setq ret LST)
  )
  ret
)
« Last Edit: March 03, 2009, 03:22:01 AM by jxphklibin »

jxphklibin

  • Guest
Re: RemoveNth ...
« Reply #54 on: March 03, 2009, 03:19:32 AM »
Code: [Select]
[quote author=highflybird link=topic=4903.msg331499#msg331499 date=1235496262]
Here is my code.
[code](defun remove-n (n lst / a b)
  (setq a lst)
  (repeat n
    (setq b (cons (car a) b)
          a (cdr a)
    )
  )
  (setq a (cdr a))
  (foreach i b
    (setq a (cons i a))
  )
  a
)
By the way, I posted a function  that insert an element in nth position of  a list.

Code: [Select]
(defun insert-n (n element lst / a b)
  (setq a lst)
  (repeat n
    (setq b (cons (car a) b)
          a (cdr a)
    )
  )
  (setq a (cons element a))
  (foreach i b
    (setq a (cons i a))
  )
  a
)
It's very similar as the function "remove-nth".
[/quote]

Here are my revised code:

Code: [Select]
;; 修改为如下:by 小李子
(defun remove-n (n lst / a b)
  (setq a lst)
  (if (and lst (>= n 0) (< n (length lst)))
    (progn
      (repeat n
(setq b (cons (car a) b)
      a (cdr a)
)
      )
      (setq a (cdr a))
      (foreach i b
(setq a (cons i a))
      )
    )
  ) ;end if
  a
)

Code: [Select]
;; 修改为如下代码:by 小李子
(defun insert-n (n element lst / a b)
  (setq a lst)
  (if (and (>= n 0) (<= n (length a)))
    (progn
      (repeat n
(setq b (cons (car a) b)
      a (cdr a)
)
      )
      (setq a (cons element a))
      (foreach i b
(setq a (cons i a))
      )
    )
  )
  a
)
[/code]
« Last Edit: March 03, 2009, 03:24:09 AM by jxphklibin »

vladimirzm

  • Guest
Re: RemoveNth ...
« Reply #55 on: March 04, 2009, 09:53:15 AM »
(ACET-LIST-REMOVE-NTH 2 '("a" "b" "c" "d" "e" "a" "a" "b" "i" "b" "k"))
> ("a" "b" "d" "e" "a" "a" "b" "i" "b" "k")