Author Topic: remove a item from list  (Read 4922 times)

0 Members and 1 Guest are viewing this topic.

subbup

  • Guest
remove a item from list
« on: June 29, 2004, 07:07:19 AM »
I know the position of an item in a list.
Till that position I want to remove all the items.
Is there any function to do this.

hendie

  • Guest
remove a item from list
« Reply #1 on: June 29, 2004, 07:13:57 AM »
have you looked into vl-remove ?

subbup

  • Guest
remove a item from list
« Reply #2 on: June 29, 2004, 07:27:03 AM »
It's removing all the duplicates in a list.
but I want to delete upto particular position in a list.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
remove a item from list
« Reply #3 on: June 29, 2004, 07:44:44 AM »
this is as beautiful as some but.......
Code: [Select]

(setq lst (list 23 23 43 54 90 77 88 99))
(23 23 43 54 90 77 88 99)

; remove the first three elements
(repeat 2 (setq lst (vl-remove (nth 0 lst) lst)))
(54 90 77 88 99)
TheSwamp.org  (serving the CAD community since 2003)

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
remove a item from list
« Reply #4 on: June 29, 2004, 08:34:44 AM »
Using the same list as Mark:
Code: [Select]

(defun list_trim (lst posit / )
  (repeat posit (setq lst (cdr lst)))
  lst
  )

_$ (setq lst (list 23 23 43 54 90 77 88 99))
(23 23 43 54 90 77 88 99)
_$ (list_trim lst 3)
(54 90 77 88 99)
_$ (list_trim lst 5)
(77 88 99)
_$ (list_trim lst 2)
(43 54 90 77 88 99)
_$ (list_trim lst 1)
(23 43 54 90 77 88 99)

BTW Mark, can you see why your code removes the first three elements of your list when you only run repeat twice?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
remove a item from list
« Reply #5 on: June 29, 2004, 09:59:23 AM »
Hay that's fun. :)
And useful too.
Code: [Select]

(defun list_Rtrim (lst posit / )
  (setq lst (reverse lst))
  (repeat posit (setq lst (cdr lst)))
  (reverse lst )
)

(defun list_Ltrim (lst posit / )
  (repeat posit (setq lst (cdr lst)))
)

(defun list_trim (lst left right / )
  (list_Ltrim (list_Rtrim lst right) left)
)



Code: [Select]

(setq lst (list 0 1 2 3 4 5 6 7 8 9))

_$ (list_ltrim lst 4)
(4 5 6 7 8 9)
_$ (list_rtrim lst 4)
(0 1 2 3 4 5)
_$ (list_trim lst 2 3)
(2 3 4 5 6)
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.

SMadsen

  • Guest
remove a item from list
« Reply #6 on: June 29, 2004, 10:05:30 AM »
Heh
.. and the recursive version?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
remove a item from list
« Reply #7 on: June 29, 2004, 10:18:48 AM »
You know that makes my head hurt. :oops:
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
remove a item from list
« Reply #8 on: June 29, 2004, 10:27:39 AM »
Something like this?

Code: [Select]
(defun list_Ltrim (lst posit / )
  (if (> posit 1)
    (list_Ltrim (cdr lst) (1- posit))
    (cdr 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.

SMadsen

  • Guest
remove a item from list
« Reply #9 on: June 29, 2004, 10:44:55 AM »
Almost. What happens with (list_Ltrim alist 0)?

Or even (list_Ltrim alist -1)? I know that shouldn't be an issue but at least it shouldn't return (cdr alist)

I'd do it something like this:
Code: [Select]
(defun list_Ltrim (lst posit)
  (cond ((null lst) nil)
        ((> posit 0) (list_Ltrim (cdr lst) (1- posit)))
        (lst)
  )
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
remove a item from list
« Reply #10 on: June 29, 2004, 11:53:06 AM »
Ah yes, the old fox does it again.
I like it, just wish i thought of it. :)
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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10652
remove a item from list
« Reply #11 on: June 29, 2004, 12:05:24 PM »
*Psst* Look over here.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
remove a item from list
« Reply #12 on: June 29, 2004, 05:56:55 PM »
I think what's been posted, particularly:

Code: [Select]

(defun list_Ltrim (lst posit)
  (cond ((null lst) nil)
        ((> posit 0) (list_Ltrim (cdr lst) (1- posit)))
        (lst)
  )
)

are very good solutions, and generally follow the approach I'd take.

But solely for fun, obfuscation and a demonstration of "thinking outside the box" consider why this works (code distilled to re-useable functions):

Code: [Select]

(defun MakeKeys ( lst / key )
    (setq key -1)
    (mapcar '(lambda (item) (setq key (1+ key))) lst)
)

(defun IndexList ( lst )
    (mapcar 'cons
        (MakeKeys lst)
        lst
    )
)

(defun LeftTrimList ( lst n / index )
    (if (< n 1) lst
        (mapcar 'cdr
            (member
                (assoc n (setq index (IndexList lst)))
                index
            )  
        )
    )
)

(setq lst '(A B C D E F G H I))

(LeftTrimList lst 5)  =>  (F G H I)

(LeftTrimList lst -1)  =>  (A B C D E F G H I)

(LeftTrimList lst 99)  =>  nil

Cheers. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

SMadsen

  • Guest
remove a item from list
« Reply #13 on: June 29, 2004, 06:08:10 PM »
Interesting approach, Michael. Quite a detour but instructive

So, a righttrim function would be
Code: [Select]
(defun RightTrimList (lst n / index)
  (if (< n 1) lst
    (reverse (mapcar 'cdr (member
                (assoc n (setq index (IndexList (reverse lst))))
                index))
    )
  )
)


Right?

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
remove a item from list
« Reply #14 on: June 29, 2004, 06:11:38 PM »
Quote from: SMadsen
Interesting approach, Michael. Quite a detour but instructive

Thank you Stig, just a knurd havin' fun. :)

<Didn't test your variant but it looks just about right ro me.>
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst