TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: JohnK on January 19, 2004, 12:10:17 PM

Title: Renamed -> (challenge) Remove an item from a List.
Post by: JohnK 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!
Title: Renamed -> (challenge) Remove an item from a List.
Post by: daron 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.
Title: Renamed -> (challenge) Remove an item from a List.
Post by: daron on January 19, 2004, 12:18:58 PM
Or did you want to gather all the three's from the list?
Title: Renamed -> (challenge) Remove an item from a List.
Post by: Columbia 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...
Title: Renamed -> (challenge) Remove an item from a List.
Post by: daron 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.
Title: Renamed -> (challenge) Remove an item from a List.
Post by: JohnK 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.
Title: Renamed -> (challenge) Remove an item from a List.
Post by: Craig 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)
Title: Renamed -> (challenge) Remove an item from a List.
Post by: Keith™ 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 :)
Title: Renamed -> (challenge) Remove an item from a List.
Post by: SMadsen 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))))
  )
)
Title: Renamed -> (challenge) Remove an item from a List.
Post by: Keith™ on January 19, 2004, 02:14:51 PM
Now there is a unique perspective ....
Title: Renamed -> (challenge) Remove an item from a List.
Post by: JohnK 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.
Title: Renamed -> (challenge) Remove an item from a List.
Post by: SMadsen 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 (http://green.iis.nsk.su/~vp/doc/lisp1.5/node8.html):
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]]


:)
Title: Renamed -> (challenge) Remove an item from a List.
Post by: JohnK on January 19, 2004, 03:08:52 PM
Thats even cooler!?
Title: Renamed -> (challenge) Remove an item from a List.
Post by: Mark 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)
  )
Title: Renamed -> (challenge) Remove an item from a List.
Post by: SMadsen 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))
Title: Renamed -> (challenge) Remove an item from a List.
Post by: rude dog on January 20, 2004, 10:18:17 PM
been so busy with my post missed this one well here it goes....(p.s sorry about the caps daron...ha ha)
Command: (setq lst '(1 2 3 4 5 6 7 8 9 0 3 3 3 3))
(1 2 3 4 5 6 7 8 9 0 3 3 3 3)
Command: (SETQ NL1 (list (CADDR LST)))
(3)
Command: (SETQ NL (member '0 lst))
(0 3 3 3 3)
Command: (SETQ NL2 (CDR NL))
(3 3 3 3)
Command: (SETQ NL3 (APPEND NL1 NL2))
(3 3 3 3 3)
Command: !NL3
(3 3 3 3 3)
Title: Renamed -> (challenge) Remove an item from a List.
Post by: JohnK on January 21, 2004, 10:00:21 AM
Well this was kind fun.

Wanna do another? (I think i have another cool idea.)  Im super busy but ill try my best to get some code down and participate but i cant promise anythning. (Damn microstation!)
Title: Renamed -> (challenge) Remove an item from a List.
Post by: SMadsen on January 21, 2004, 10:00:52 AM
Bring it on!
Title: Renamed -> (challenge) Remove an item from a List.
Post by: Matt Stachoni on January 28, 2004, 12:20:03 PM
I know I'm late to the party, but how about:
(defun list:Remove (e l)
  (apply 'append (subst nil (list e) (mapcar 'list l)))
)
Title: Renamed -> (challenge) Remove an item from a List.
Post by: daron on January 28, 2004, 12:33:41 PM
Interesting.
Title: Renamed -> (challenge) Remove an item from a List.
Post by: SMadsen on January 28, 2004, 01:12:41 PM
Nice one, Matt!
Title: Renamed -> (challenge) Remove an item from a List.
Post by: JohnK on January 29, 2004, 12:06:29 PM
Quote from: Matt Stachoni
I know I'm late to the party, but how about:
(defun list:Remove (e l)
(apply 'append (subst nil (list e) (mapcar 'list l)))
)


Sweeet banana's?! Dahell is that?! Ummm... Mat, Could you be a little more efficent with your code?! :P
Title: Renamed -> (challenge) Remove an item from a List.
Post by: Anonymous on January 30, 2004, 10:17:28 AM
Quote from: Se7en

Sweeet banana's?! Dahell is that?! Ummm... Mat, Could you be a little more efficent with your code?! :P


Well, that's the name of the game, right?
Title: Renamed -> (challenge) Remove an item from a List.
Post by: daron on January 30, 2004, 10:55:21 AM
Matt, are you going to register and join us here? We'd really like to see you around more. There are some perks for joining. It is busier here than a guest can tell.
Title: Renamed -> (challenge) Remove an item from a List.
Post by: JohnK on January 30, 2004, 11:10:14 AM
Quote from: Anonymous
Well, that's the name of the game, right?
lol! Yep. you took the cake with that lil bit of code. Great peice of work man.
Title: Renamed -> (challenge) Remove an item from a List.
Post by: Matt Stachoni on January 30, 2004, 01:31:20 PM
Yep, I just registered. This is a very cool discussion group, a nice distraction from the usual Autodesk discussion forums.
Title: Renamed -> (challenge) Remove an item from a List.
Post by: daron on January 30, 2004, 01:34:02 PM
fffssssss. Ouch. hehe.