Lee Mac Posted April 25, 2009 Posted April 25, 2009 Just wondered if there was a better way to approach this than what I have currently: [b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] remove_items [b][color=RED]([/color][/b]lst items[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]vl-remove-if[/color][/b] [b][color=DARKRED]'[/color][/b][b][color=RED]([/color][/b][b][color=BLUE]lambda[/color][/b] [b][color=RED]([/color][/b]j[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]member[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]vl-position[/color][/b] j lst[b][color=RED])[/color][/b] items[b][color=RED])[/color][/b][b][color=RED])[/color][/b] lst[b][color=RED])[/color][/b][b][color=RED])[/color][/b] Quote
David Bethel Posted April 25, 2009 Posted April 25, 2009 Lee, Here is very old and fairly slow way to delete an atom from a list: [b][color=BLACK]([/color][/b]defun delatom [b][color=FUCHSIA]([/color][/b]a l / tmp[b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]while l [b][color=NAVY]([/color][/b]if [b][color=MAROON]([/color][/b]not [b][color=GREEN]([/color][/b]equal a [b][color=BLUE]([/color][/b]car l[b][color=BLUE])[/color][/b] 1e-11[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b]setq tmp [b][color=GREEN]([/color][/b]cons [b][color=BLUE]([/color][/b]car l[b][color=BLUE])[/color][/b] tmp[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]setq l [b][color=MAROON]([/color][/b]cdr l[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]reverse tmp[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b] [b][color=BLACK]([/color][/b]setq tl '[b][color=FUCHSIA]([/color][/b][b][color=NAVY]([/color][/b]0 . [color=#2f4f4f]"LINE"[/color][b][color=NAVY])[/color][/b] 234 [color=#2f4f4f]"ABC"[/color] 456.78 sym [b][color=NAVY]([/color][/b]10 0 0 1[b][color=NAVY])[/color][/b] [color=#2f4f4f]"DEF"[/color][b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b] [b][color=BLACK]([/color][/b]prin1 [b][color=FUCHSIA]([/color][/b]delatom [color=#2f4f4f]"ABC"[/color] tl[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b] And a time test routine: [b][color=BLACK]([/color][/b]defun Diesel [b][color=FUCHSIA]([/color][/b]c[b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]menucmd [b][color=NAVY]([/color][/b]strcat [color=#2f4f4f]"M="[/color] c[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b] [b][color=BLACK]([/color][/b]defun c:timetest [b][color=FUCHSIA]([/color][/b]/ st ft[b][color=FUCHSIA])[/color][/b] [color=#8b4513];| Set Any Variables Here |;[/color] [b][color=FUCHSIA]([/color][/b]setq st [b][color=NAVY]([/color][/b]getvar [color=#2f4f4f]"DATE"[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [color=#8b4513];;; Preform all tests in this section[/color] [b][color=FUCHSIA]([/color][/b]repeat 1000 [color=#8b4513];|Vary repeat parameter with complexity of test call|;[/color] [b][color=NAVY]([/color][/b]delatom [color=#2f4f4f]"ABC"[/color] tl[b][color=NAVY])[/color][/b] [b][color=FUCHSIA])[/color][/b][color=#8b4513];End Repeat[/color] [color=#8b4513];| Have all functions tested ended by here |;[/color] [b][color=FUCHSIA]([/color][/b]setq ft [b][color=NAVY]([/color][/b]getvar [color=#2f4f4f]"DATE"[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]setvar [color=#2f4f4f]"USERR1"[/color] [b][color=NAVY]([/color][/b]- ft st[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]princ [b][color=NAVY]([/color][/b]diesel [b][color=MAROON]([/color][/b]strcat [color=#2f4f4f]"$[b][color=GREEN]([/color][/b]edtime,$[b][color=BLUE]([/color][/b]getvar,userr1[b][color=BLUE])[/color][/b],MM\"[/color]:\[color=#2f4f4f]"SS\"[/color].\[color=#2f4f4f]"MSEC[b][color=GREEN])[/color][/b]"[/color][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]princ 1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b] -David Quote
Lee Mac Posted April 25, 2009 Author Posted April 25, 2009 Thanks David, I had originally thought of shuffling through the list, using a (setq lst (cdr lst)) method, but I found that this would only be useful for removing a single item, and if there are duplicates in the list, it would remove both instances of the duplicate, instead of the one requested. PS, I like the time-test routine... very novel. Quote
taner Posted April 26, 2009 Posted April 26, 2009 (defun remove-lst-items (lst itemlst / i) (setq i -1) (vl-remove-if '(lambda (x) (member (setq i (1+ i)) itemlst ) ) lst ) ) Quote
CAB Posted April 27, 2009 Posted April 27, 2009 Other methods: http://www.theswamp.org/index.php?topic=28100.msg336941#msg336941 Quote
Lee Mac Posted April 27, 2009 Author Posted April 27, 2009 Wow It seems the swamp has much more advanced methods than here... tons of recursive functions (they make my head hurt). Quote
Se7en Posted April 27, 2009 Posted April 27, 2009 Nah, dont let it hurt. Recursion is the simplest loop you can make. Quote
Lee Mac Posted April 27, 2009 Author Posted April 27, 2009 I just find it hard to work out what the function is doing when you are using a recursive process. Quote
Se7en Posted April 27, 2009 Posted April 27, 2009 Maybe some code with comments will help enlighten. With recursion we can itterate thru a list. For example we can build a simple recursive procedure to copy a list. Read the comments to better understand the "Flow" of the procedure durring its operation. (defun copy-tree (x) ;; Return an exact copy of a given list. ;; (nested lists are this procedures speciality.) ;; ;; EX: (copy-tree '(1 2 (3 (4 5) 6 (nil)) 7 (())) ;; > (1 2 (3 (4 5) 6 (nil)) 7 (()) (if (atom x) ; If "x" is only an item, not a list, x ; return the item...else, (cons (copy-tree (car x)) ; build a list, sending the next item ; back thru itself along with (copy-tree (cdr x)) ; the remander of the items, ; via backtrace. ; ; NOTE: ; last fuction `cons' returns assembled ; list of items or exact copy of what ; was passed to this procedure. ) ) ) Using the above we can build a slightly modified version to incriment the numbers in that list. (defun copy-incriment-tree (x) ;; Return a list contaning numbers incrimented by one. ;; (nested lists are this procedures speciality.) ;; ;; NOTE: This procedure will only incriment numbers, it will ;; leave any non number member of the given list alone. ;; ;; Ex: (copy-incriment-tree '(1 2 (3 (4 5) 6 (nil "a" 1.225)) 7 (())) ;; > (2 3 (4 (5 6) 7 (nil "a" 2.225)) 8 ((9))) (if (atom x) (if (numberp x) ;; if its a number, we know we can add one to it. (1+ x) ;; if the numberp condition fails then its not a number ;; so just return the item x ) (cons (copy-incriment-tree (car x)) (copy-incriment-tree (cdr x)) ) ) ) Does that help? Quote
Lee Mac Posted April 27, 2009 Author Posted April 27, 2009 That helps a little more. I suppose it acts like a WHILE statement that iterates through a list until there is no list left. Many thanks John, for your time and patience. Cheers Lee Quote
Se7en Posted April 27, 2009 Posted April 27, 2009 yeah sorta. However a WHILE (or other looping constructs) puts less strain on the memory stack...but that's a good enough thought process for now. "Recursion is a simple way to do something to items of a list". No problem, i like to help. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.