transcad Posted April 5, 2012 Posted April 5, 2012 How can i remove element number 6 from a list of 20 elements? Quote
MSasu Posted April 5, 2012 Posted April 5, 2012 If the entry is unique, then use VL-REMOVE function. That 6 is the index of entry? (vl-remove (nth 6 myList) myList) Other, start an empty list and copy the items from source one except the item you want removed; suggested functions to use: REPEAT, LENGTH, APPEND or CONS. Regards, Mircea Quote
VVA Posted April 5, 2012 Posted April 5, 2012 http://www.theswamp.org/index.php?topic=14226.0 http://www.theswamp.org/index.php?topic=14170.0 http://www.lee-mac.com/removenth.html one of the variant (defun remove-i (i lst) ;;; i - index (fom 0) ;;;lst- list of elemets (setq i (1+ i)) (vl-remove-if '(lambda (x) (zerop (setq i (1- i)))) lst) ) ;_ end of defun Quote
Tharwat Posted April 5, 2012 Posted April 5, 2012 (vl-remove (nth 6 myList) myList) nth function consider zero as the first element Quote
MSasu Posted April 5, 2012 Posted April 5, 2012 nth function consider zero as the first element Thus my question if 6 is the index of item or not... Regards, Mircea Quote
marko_ribar Posted April 5, 2012 Posted April 5, 2012 (defun remove_nth ( lst n / lstn ) (setq n (1+ n)) (mapcar (function (lambda (x) (if (not (zerop (setq n (1- n)))) (setq lstn (cons x lstn))))) lst) (reverse lstn) ) M.R. Quote
Lee Mac Posted April 5, 2012 Posted April 5, 2012 Marko, Since you are constructing the return list independent of the mapcar expression (i.e. you are not using the return of mapcar), I would be inclined to use foreach instead, i.e.: (defun remove_nth ( lst n / lstn ) (setq n (1+ n)) (foreach x lst (if (/= 0 (setq n (1- n))) (setq lstn (cons x lstn)))) (reverse lstn) ) Here are my alternatives for this task: Remove Nth Remove Items Quote
pBe Posted April 6, 2012 Posted April 6, 2012 I've been using this until i learned about vl-remove-if (defun RemNth (n l / i nl m) (repeat (setq i n) (setq nl (cons (nth (setq i (1- i)) l) nl))) (repeat ( - (setq i (length l)) n) (setq m (cons (nth (setq i (1- i)) l) m)) ) (append nl (cdr m)) ) @Marko: Nice code @LM: Recursive still makes my head spin. Need to focus on that one of these days. Quote
irneb Posted April 6, 2012 Posted April 6, 2012 (edited) @LM: Recursive still makes my head spin. Need to focus on that one of these days. Ah! It's not too difficult : You squint a bit, and because you squint you squint some more, and that makes you squint even harder ... until finally you squint so much you're looking at the back of your head! Edit: Hang on, Lee didn't use recursion there at all. That's a straight forward iteration. This would be recursion (though I wouldn't recommend it on any lists longer than 10000): (defun RemNth-R (n lst /) (if (> n 0) (cons (car lst) (RemNth-R (1- n) (cdr lst))) (cdr lst))) As an explanation of what happens in this example: _$ (RemNth-R 3 '(0 1 2 3 4 5 6 7 8 9)) (0 1 2 4 5 6 7 8 9) Call with index of 3, check index > 0, construct 1st item with result of Call again with index-1 and remainder of list (Squint a bit)Called with index=2, check index > 0, construct 1st item with result of Call again with index-1 and remainder of list (Squint some more)Called with index=1, check index > 0, construct 1st item with result of Call again with index-1 and remainder of list (Squint harder)Called with index=0, check index not > 0, return remainder of list (You're looking at the back of your head) [*]Use the remainder returned in construction of new list (So now everything works backwards) [*]Use the remainder returned in construction of new list (Still not out of the dream world) [*]Use the remainder returned in construction of new list (I think this is the last door) Finally the returned list. Edited April 6, 2012 by irneb Quote
pBe Posted April 6, 2012 Posted April 6, 2012 Ah! It's not too difficult : You squint a bit, and because you squint you squint some more, and that makes you squint even harder ... until finally you squint so much you're looking at the back of your head! That sorta sums it up for me irneb Quote
irneb Posted April 6, 2012 Posted April 6, 2012 That sorta sums it up for me irneb Thought you'd like it ... though you might be surprised. That's actually what recursion is doing to itself - see the modification in my previous post. Quote
Lee Mac Posted April 6, 2012 Posted April 6, 2012 Hang on, Lee didn't use recursion there at all. I think pBe was referring to this Great minds think alike it seems Irneb ;-) Quote
irneb Posted April 6, 2012 Posted April 6, 2012 Great minds think alike it seems Irneb ;-) I promise I didn't even look at your code before posting mine. It's uncanny! Thanks for the veiled compliment though! Quote
pBe Posted April 6, 2012 Posted April 6, 2012 Thought you'd like it ... though you might be surprised. That's actually what recursion is doing to itself - see the modification in my previous post. pBe still at awe ---- > Thanks a bunch irneb I think pBe was referring to this Yup, thats the one. Really appreciate you guys Cheers Quote
wishbonesr Posted August 3, 2012 Posted August 3, 2012 Here's a few more optimized list manipulators since the topic kiiiinda shifted that way. I know the one looks just like Lee's/irneb's... (defun nth-replace ( newitem alist position / i ) (setq i -1) (mapcar (function (lambda ( x ) (if (= position (setq i (1+ i))) newitem x))) alist) ) (defun vl:nth-remove (alist position / i) (setq i -1) (vl-remove-if (function (lambda (x) (= position (setq i (1+ i))))) alist) ) (defun vl:nth-add (newitem alist position / i p) (setq i -1 p -1) (append (vl-remove-if (function (lambda (x) (<= position (setq p (1+ p))))) alist) (list newitem) (vl-remove-if (function (lambda (x) (> position (setq i (1+ i))))) alist) ) ) 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.