Author Topic: (challenge) nth remover  (Read 16534 times)

0 Members and 1 Guest are viewing this topic.

SMadsen

  • Guest
(challenge) nth remover
« Reply #30 on: January 21, 2004, 06:02:23 PM »
Didn't say it wasn't a smart way to do it. It is smart!

SMadsen

  • Guest
(challenge) nth remover
« Reply #31 on: January 21, 2004, 06:08:59 PM »
Quote from: Keith
Did you notice that it didn't require the setting of any additional variables.
Yep. Slick and short. Limited to first occurence of an item but slick and short nonetheless.

If only there was a NTH_MEMBER function. That's why the idea of (repeat i (setq lst (cdr lst))) - only it didn't turn out as clean as yours.

daron

  • Guest
(challenge) nth remover
« Reply #32 on: January 21, 2004, 06:27:11 PM »
I was trying to use member at first. I wanted the member of the nth + 1 part of the list. That was easy, but the first half is where I was having trouble; gathering the list 1- to the nth of the list.

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
(challenge) nth remover
« Reply #33 on: January 21, 2004, 09:22:11 PM »
Quote from: SMadsen
If only there was a NTH_MEMBER function. That's why the idea of (repeat i (setq lst (cdr lst))) - only it didn't turn out as clean as yours.


Like this?
Code: [Select]
(defun nthMember (lst item / cntr pos)
  (mapcar
    '(lambda (x)
      (if (not cntr) (setq cntr 0) (setq cntr (1+ cntr)))
       (if (eq x item)
         (setq pos cntr)
         )
       )
    lst
    )
  pos
 )

Command: (nthmember '(1 2 3 4) 2)
> 1
Command: (nthmember '(1 2 3 4) 1)
> 0
Command: (nthmember '(1 2 3 4) 6)
> nil
Command: (nthmember '(1 2 3 4) 3)
> 2
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
(challenge) nth remover
« Reply #34 on: January 22, 2004, 04:26:42 AM »
Nah, I meant more like: get the rest of the list from nth index

Code: [Select]
(defun nthmem (lst i)
  (cond ((zerop i) lst)
        ((repeat i (setq lst (cdr lst))))
  )
)


(nthmem '(0 1 2 3 4 5) 2)
(2 3 4 5)
(nthmem '(0 1 2 3 4 5) 0)
(0 1 2 3 4 5)
(nthmem '(0 1 2 3 4 5) 4)
(4 5)
(reverse (nthmem (reverse '(0 1 2 3 4 5)) 4))
(0 1)

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
(challenge) nth remover
« Reply #35 on: January 22, 2004, 09:21:08 AM »
Oh, Got cha. ...hey thats cool.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(challenge) nth remover
« Reply #36 on: January 22, 2004, 09:31:52 AM »
NEXT..................

since i didn't get to play in this one, i'm ready for another. :D
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
(challenge) nth remover
« Reply #37 on: January 22, 2004, 09:39:44 AM »
Another?!  ...Ok let me try to think up another idea.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org