Jump to content

Recommended Posts

Posted

How can i remove element number 6 from a list of 20 elements?

Posted

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

Posted

(vl-remove (nth 6 myList) myList)

 

 

nth function consider zero as the first element ;)

Posted
nth function consider zero as the first element ;)

 

Thus my question if 6 is the index of item or not...

 

Regards,

Mircea

Posted

(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.

Posted

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

Posted

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 :thumbsup:

 

@LM: Recursive still makes my head spin. Need to focus on that one of these days. :)

Posted (edited)
@LM: Recursive still makes my head spin. Need to focus on that one of these days. :)
Ah! It's not too difficult o:): 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! :shock:

 

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 by irneb
Posted
Ah! It's not too difficult o:): 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! :shock:

 

That sorta sums it up for me irneb :rofl:

Posted
That sorta sums it up for me irneb :rofl:
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.
Posted
Great minds think alike it seems Irneb ;-)
:shock: I promise I didn't even look at your code before posting mine. It's uncanny! Thanks for the veiled compliment though! :notworthy:
Posted
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 ---- > :o

 

Thanks a bunch irneb

 

I think pBe was referring to this

 

Yup, thats the one. :thumbsup:

 

Really appreciate you guys :notworthy:

 

Cheers

  • 3 months later...
Posted

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)
  )
 )

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...