Author Topic: Renamed -> (challenge) Remove an item from a List.  (Read 10780 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Renamed -> (challenge) Remove an item from a List.
« on: January 19, 2004, 12:10:17 PM »
Your, mission if you choose to accept it, is to build a procedure to take a given argument out of a list. (No matter where it is in that list) Ill give you a demonstration.

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

There is your list, i want you to develop a small procedure that will remove all the three's out of that list. Do this anyway you know how. Or think up the "Coolest" way you can. Oh and try to keep track of your time you do this too. Did it take you half an hour to think of a way or did it take you five min. (...AND DONT LIE! )

I already have an idea! w00t!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

daron

  • Guest
Renamed -> (challenge) Remove an item from a List.
« Reply #1 on: January 19, 2004, 12:14:52 PM »
Two minutes, roughly.
Code: [Select]
(defun numrem (lst)
     ;;(setq lst '(1 2 3 4 5 6 7 8 9 0 3 3 3 3))
     (vl-remove 3 lst)
     (princ)
)


That included making the list and checking it twice. Finding out if I missed something in your quest(ion). Really, two seconds to think vl-remove and a few seconds to make it a function, check the help file to make sure I was using vl-remove correctly.

daron

  • Guest
Renamed -> (challenge) Remove an item from a List.
« Reply #2 on: January 19, 2004, 12:18:58 PM »
Or did you want to gather all the three's from the list?

Columbia

  • Guest
Renamed -> (challenge) Remove an item from a List.
« Reply #3 on: January 19, 2004, 12:21:50 PM »
Code: [Select]


(defun Remove_Atom (itm lst / i rtn_list)
  (setq i 0)
  (repeat (length lst)
    (if (/= (nth i lst) itm)
      (setq rtn_list (append rtn_list (list (nth i lst))))
    )
    (setq i (1+ i))
  )
  rtn_list
)



Straight AutoLISP... ha ha ... take that :) ... less than 2 minutes...

daron

  • Guest
Renamed -> (challenge) Remove an item from a List.
« Reply #4 on: January 19, 2004, 12:24:48 PM »
Code: [Select]
(defun numrem (lst litem)
     ;;(setq lst '(1 2 3 4 5 6 7 8 9 0 3 3 3 3))
     (foreach item lst
 (if (= item litem)
      (setq three (append three (list item)))
 )
     )
     (princ)
)

To remove all items from a list except specified list item. Time unknown. Within the last few minutes though.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Renamed -> (challenge) Remove an item from a List.
« Reply #5 on: January 19, 2004, 12:48:50 PM »
Code: [Select]
(defun RemoveFromList (Lst Item / NewLst)
  (mapcar
    '(lambda (x)
       (if (/= x item)
         (setq NewLst (cons x NewLst))))
    Lst
    )
  (reverse newlst)
 )


I had a couple of min too but some people here actualy want to do "work" :? But here is my method. Straight alisp.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Craig

  • Guest
Renamed -> (challenge) Remove an item from a List.
« Reply #6 on: January 19, 2004, 01:24:22 PM »
This just goes to show you, there's more than one way to skin a cat  8)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Renamed -> (challenge) Remove an item from a List.
« Reply #7 on: January 19, 2004, 01:56:24 PM »
Well shoot ... look what happens when you show up late to the party ... you miss all of the fun ....

Oh, here are some observations ...

This code in not in compliance with "The Act" and will only work on VB6, does not return a value to the calling function, and does not set a retrievable value within the code.
Quote from: Daron

Code: [Select]

(defun numrem (lst)
     ;;(setq lst '(1 2 3 4 5 6 7 8 9 0 3 3 3 3))
     (vl-remove 3 lst)
     (princ)
)



This code is simple, is in full compliance with "The Act" and will work with R12 through R2004, it also returns a value to the calling function and localizes all variables to prevent conflicts with other code.
Quote from: Columbia

Code: [Select]

(defun Remove_Atom (itm lst / i rtn_list)
  (setq i 0)
  (repeat (length lst)
    (if (/= (nth i lst) itm)
      (setq rtn_list (append rtn_list (list (nth i lst))))
    )
    (setq i (1+ i))
  )
  rtn_list
)



This code is simple and concise, will run on R12 through R2004 and sets a variable that can be retrieved by the calling function. It does not however, return a value to the calling function, nor does it make use of local variables.
Quote from: Daron

Code: [Select]

(defun numrem (lst litem)
     ;;(setq lst '(1 2 3 4 5 6 7 8 9 0 3 3 3 3))
     (foreach item lst
     (if (= item litem)
          (setq three (append three (list item)))
     )
     )
     (princ)
)



This code is simple and concise, utilizes localized variables and returns a value to the calling function and is in full compliance with "The Act".
Quote from: Se7en

Code: [Select]

(defun RemoveFromList (Lst Item / NewLst)
  (mapcar
    '(lambda (x)
       (if (/= x item)
         (setq NewLst (cons x NewLst))))
    Lst
    )
  (reverse newlst)
 )



I think Se7en wins his own prize ....

But then I was too slow to respond :)
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

SMadsen

  • Guest
Renamed -> (challenge) Remove an item from a List.
« Reply #8 on: January 19, 2004, 02:12:30 PM »
Took a bit of testing as not to rewind the list:

Code: [Select]
(defun remItem (item lst)
  (cond ((null lst) lst)
        ((= (car lst) item) (remItem item (cdr lst)))
        ((cons (car lst) (remItem item (cdr lst))))
  )
)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Renamed -> (challenge) Remove an item from a List.
« Reply #9 on: January 19, 2004, 02:14:51 PM »
Now there is a unique perspective ....
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

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Renamed -> (challenge) Remove an item from a List.
« Reply #10 on: January 19, 2004, 02:24:24 PM »
Darn it! I was gonna re-write mine to be a recursive procedure.  ...that's slick Stig. I would have never thought of that.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
Renamed -> (challenge) Remove an item from a List.
« Reply #11 on: January 19, 2004, 02:43:38 PM »
John,
Not sure I wanna take credit for it, though. I was just reading up on McCarthy et al.'s old LISP 1.5 manual and was messing around with some of the recursive stuff. E.g. his Intersection and Union, which are very much like the remove-item thingie:

Code: [Select]
(defun intersection (lst1 lst2)
  (cond ((null lst1) nil)
        ((member (car lst1) lst2) (cons (car lst1) (intersection (cdr lst1) lst2)))
        (T (intersection (cdr lst1) lst2))
  )
)

(defun union (lst1 lst2)
  (cond ((null lst1) lst2)
        ((member (car lst1) lst2) (union (cdr lst1) lst2))
        ((cons (car lst1) (union (cdr lst1) lst2)))
  )
)


Of course, he used a bit of a different syntax:
Code: [Select]
union[x;y] = [null[x] -> y;
  member[car[x];y] -> union [cdr[x];y];
  T -> cons[car[x];union[cdr[x];y]]]  
 
intersection[x;y] = [null[x] -> NIL;
  member[car[x];y] -> cons[car[x];intersection[cdr[x];y]];
  T -> intersection[cdr[x];y]]


:)

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Renamed -> (challenge) Remove an item from a List.
« Reply #12 on: January 19, 2004, 03:08:52 PM »
Thats even cooler!?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Renamed -> (challenge) Remove an item from a List.
« Reply #13 on: January 20, 2004, 06:28:05 AM »
About 5 minutes. (had to read the thread to make sure no one else used it)
Code: [Select]

(defun rfl (lst item)
  (vl-remove-if '(lambda (x) (eq x item)) lst)
  )
TheSwamp.org  (serving the CAD community since 2003)

SMadsen

  • Guest
Renamed -> (challenge) Remove an item from a List.
« Reply #14 on: January 20, 2004, 07:14:31 AM »
Quote from: Mark Thomas
Code: [Select]

(defun rfl (lst item)
  (vl-remove-if '(lambda (x) (eq x item)) lst)
  )


Whoops, that reminds me to use EQUAL and not '=' for things like this. Thanks Mark (but your code suffers from it, too) :)
Code: [Select]
(defun remItem (item lst)
  (cond ((null lst) lst)
        ((equal (car lst) item) (remItem item (cdr lst)))
        ((cons (car lst) (remItem item (cdr lst))))
  )
)


With '=' or EQ (not good if item to remove is a list itself):
(remitem '(3) '((1)(2)(3)(3)(4))) -> ((1)(2)(3)(3)(4))

With EQUAL:
(remitem '(3) '((1)(2)(3)(3)(4))) -> ((1) (2) (4))