Author Topic: help (remove exactly one item from a list)  (Read 4943 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
help (remove exactly one item from a list)
« on: February 02, 2004, 12:07:30 PM »
I need to remove the nth item from a list. What the item is does not matter just the position of the item in the list. Make sense?
Example:
Code: [Select]

(setq lst '(1 2 3 6 7 8))
(1 2 3 6 7 8)

Now remove the 4th item leaving me with this;
Code: [Select]
(1 2 3 7 8)
TheSwamp.org  (serving the CAD community since 2003)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
help (remove exactly one item from a list)
« Reply #1 on: February 02, 2004, 12:29:57 PM »
Code: [Select]

(defun listItem:Remove (e l)
(apply 'append (subst nil (list(nth e l))(mapcar 'list l)))
)


Concept borrowed from Matt ...
Thanks Matt .. :wink:
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

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
help (remove exactly one item from a list)
« Reply #2 on: February 02, 2004, 12:52:30 PM »
- Keith
That removes all the items matching 'e' I want to remove just the nth item from the list.
TheSwamp.org  (serving the CAD community since 2003)

daron

  • Guest
help (remove exactly one item from a list)
« Reply #3 on: February 02, 2004, 12:56:39 PM »
So, none of the lisps that were submitted as part of Se7en's challenge work quite right?

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
help (remove exactly one item from a list)
« Reply #4 on: February 02, 2004, 01:03:50 PM »
nope, unless I'm missing something.
TheSwamp.org  (serving the CAD community since 2003)

Matt Stachoni

  • Guest
help (remove exactly one item from a list)
« Reply #5 on: February 02, 2004, 01:06:52 PM »
Removing the nth item from a list (untested):

Code: [Select]
(defun rnth (l i / c e)
  ;; l = list
  ;; i = index (0-based)
  (setq c 0)
  (while (setq e (nth c l))
    (if (/= i c) (setq rl (cons e rl))
    (setq c (1+ c))
  )
  (reverse rl)
)

(rnth '(0 1 2 3 4) 3) -> '(0 1 2 4)

SMadsen

  • Guest
help (remove exactly one item from a list)
« Reply #6 on: February 02, 2004, 01:57:05 PM »
Mark, as far I can see all these from Se7en's challenge worked:

Code: [Select]
;; nthRemove challenge by Se7en, jan. 2004

;; Keith:
(defun NthRemover ( ndx lst / count newlist )
  (setq count 0)
  (repeat (length lst)
    (if (/= count ndx)
      (setq newlist (append newlist (list (car lst))))
    )
    (setq lst (cdr lst)
     count (1+ count)
    )
  )
  newlist
)

;; Keith:
;; Only removing if unique value to be removed!
(defun nthremove ( ndx lst )
  (append
    (reverse (cdr (member (nth ndx lst) (reverse lst))))
    (cdr (member (nth ndx lst) lst))
  )  
)

;; Kerry B:
(defun nthremover_2 (lst i / tmp)
  (if (and i (vl-consp lst) (not (minusp i)))
    (progn (repeat (min i (length lst))
             (setq tmp (cons (car lst) tmp)
                   lst (cdr lst)
             )
           )
           (append (reverse tmp) (cdr lst))
    )
    lst
  )
)

;; SMadsen:
(defun removeNth (lst i / a)
  (setq a -1)
  (vl-remove-if (function (lambda (n)(= (setq a (1+ a)) i))) lst)
)

;; SMadsen:
(defun nthRemove_2 (lst i)
  (and (atom i)(setq i (cons 0 i)))
  (cond ((null lst) nil)
        ((= (car i) (cdr i))(nthRemove_2 (cdr lst) (cons (1+ (car i)) (cdr i))))
        ((cons (car lst) (nthRemove_2 (cdr lst) (cons (1+ (car i)) (cdr i)))))
  )
)

;; SMadsen:
(defun removeNth_2 (lst i / a b)
  (cond ((>= i (length lst)) lst)
        ((setq a lst b (reverse lst))
         (append (reverse (repeat (- (length b) i) (setq b (cdr b))))
                 (repeat (1+ i) (setq a (cdr a))))
        )
  )
)

;; Se7en:
(defun nthRemover_3 (lst item / nlst cntr)
  (defun Nested-nthRemover (lst item)
    (if (/= (if (not cntr)
              (setq cntr 0)
              cntr
            )
            item
        )
      (setq nlst (cons (car lst) nlst))
    )
    (setq cntr (1+ cntr))
    (if (> (length (cdr lst)) 0)
      (Nested-nthRemover (cdr lst) item)
      (setq cntr nil)
    )
    (reverse nlst)
  )
  (Nested-nthRemover lst item)
)

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
help (remove exactly one item from a list)
« Reply #7 on: February 02, 2004, 02:15:35 PM »
My apologies, some of those do work.
TheSwamp.org  (serving the CAD community since 2003)

Matt Stachoni

  • Guest
help (remove exactly one item from a list)
« Reply #8 on: February 02, 2004, 04:48:14 PM »
Quote from: Mark Thomas
My apologies, some of those do work.


I would encourage the use of (cons) and a (reverse) instead of (append) for creating lists.

SMadsen

  • Guest
help (remove exactly one item from a list)
« Reply #9 on: February 05, 2004, 07:14:28 AM »
Just became aware that acadx.com also has a removeNth function available. Merely a variation of some of the above, though.