TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started 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!
-
Two minutes, roughly.
(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.
-
Or did you want to gather all the three's from the list?
-
(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...
-
(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.
-
(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.
-
This just goes to show you, there's more than one way to skin a cat 8)
-
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.
(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.
(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.
(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".
(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 :)
-
Took a bit of testing as not to rewind the list:
(defun remItem (item lst)
(cond ((null lst) lst)
((= (car lst) item) (remItem item (cdr lst)))
((cons (car lst) (remItem item (cdr lst))))
)
)
-
Now there is a unique perspective ....
-
Darn it! I was gonna re-write mine to be a recursive procedure. ...that's slick Stig. I would have never thought of that.
-
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:
(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):
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]]
:)
-
Thats even cooler!?
-
About 5 minutes. (had to read the thread to make sure no one else used it)
(defun rfl (lst item)
(vl-remove-if '(lambda (x) (eq x item)) lst)
)
-
(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) :)
(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))
-
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)
-
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!)
-
Bring it on!
-
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)))
)
-
Interesting.
-
Nice one, Matt!
-
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
-
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?
-
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.
-
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.
-
Yep, I just registered. This is a very cool discussion group, a nice distraction from the usual Autodesk discussion forums.
-
fffssssss. Ouch. hehe.