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

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(challenge) nth remover
« Reply #15 on: January 21, 2004, 12:28:22 PM »
>Heh, Daron's first in the trap
heyheyhey.............
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
(challenge) nth remover
« Reply #16 on: January 21, 2004, 12:30:41 PM »
Quote from: Daron
Well, I was getting a headache trying to do one non-vl, but here it is in the simplest form.
Come on Daron, Keep trying. This challege has to do with Lists --That what lisp was designed for.  ...break it down.  

ok, hows this? Picture this being a Real Life situation with Real Life objects. -e.g. you have a set of drawings and you want to remove the 5th one. How would/could you do that in RL? Translate that process to code.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
(challenge) nth remover
« Reply #17 on: January 21, 2004, 12:34:50 PM »
Sorry, didn't mean to laugh. Sorry Daron. I just expected to see it pop up among the solutions to this challenge.

I made a pretty grave error in the recursion before. Feel free to laugh very loudly and long at that one!

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
(challenge) nth remover
« Reply #18 on: January 21, 2004, 12:37:40 PM »
>I made a pretty grave error in the recursion before. Feel free to laugh very loudly and long at that one!

...yeah me too. :D
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

daron

  • Guest
(challenge) nth remover
« Reply #19 on: January 21, 2004, 12:40:14 PM »
Okay, I took to disecting this:
Code: [Select]

(defun cool (lst i / a)
  (setq a -1)
  (vl-remove-if (function (lambda (n)(= (setq a (1+ a)) i))) lst)
)

How does the lambda function work here? I see that function can be removed if stated like '(lambda (n)(= (setq a (1+ a))i))) lst) but that doesn't help.

SMadsen

  • Guest
(challenge) nth remover
« Reply #20 on: January 21, 2004, 12:59:46 PM »
VL-REMOVE-IF removes the current element of lst if the predicate-function returns anything else than nil.
The normal way of thinking about it is to evaluate each item within the LAMBDA. For example something like:

(vl-remove-if (function (lambda (n)(> n 2))) '(1 2 3 4 1 2 3 4))
-> (1 2 1 2)

But, because LAMBDA will run through every item of the list by merely supplying it as an argument, there is no need to evaluate it further.
For example, if the predicate function always returned T, it would remove each item as it is supplied to LAMBDA's argument n:

(vl-remove-if (function (lambda (n) T)) '(1 2 3 4 1 2 3 4))
-> nil

.. and if it always returned nil then no items would be removed:

(vl-remove-if (function (lambda (n) nil)) '(1 2 3 4 1 2 3 4))
-> (1 2 3 4 1 2 3 4)

To confirm that LAMBDA do evaluate each item, you could do something like:

(vl-remove-if (function (lambda (n)(princ n))) '(1 2 3 4 1 2 3 4))
-> 12341234nil

Of course, it returns nil because it removes each item (PRINC returns a null character, not nil).

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
(challenge) nth remover
« Reply #21 on: January 21, 2004, 01:01:22 PM »
Ok here is another, but it will only work with a list of unique values.
Code: [Select]

(defun nthremove ( ndx lst )
  (append
    (reverse (cdr (member (nth ndx lst) (reverse lst))))
    (cdr (member (nth ndx lst) lst))
  )  
)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

daron

  • Guest
(challenge) nth remover
« Reply #22 on: January 21, 2004, 01:03:48 PM »
You should write the help files. You do a better job of explaining it than the help files can. Thanks.

SMadsen

  • Guest
(challenge) nth remover
« Reply #23 on: January 21, 2004, 01:13:38 PM »
Thanks Daron, could you suggest that to Autod€sk? Just kidding.

Actually the last sentence wasn't correct. PRINC without argument returns a null character - otherwise is returns the evaluated argument. But in either case it's not nil. Unless it prints a nil argument .. umm, better stop now ..

daron

  • Guest
(challenge) nth remover
« Reply #24 on: January 21, 2004, 01:15:43 PM »
It seems through AUGI that you might have some pretty deep connections with Autodesk. They just don't know it yet.

SMadsen

  • Guest
(challenge) nth remover
« Reply #25 on: January 21, 2004, 01:34:36 PM »
Daron, I wouldn't know as long I can't make a buck on it  :)

Keith, that APPEND thing gave me an idea to cut out the item. So here's another:

Code: [Select]
(defun removeNth (lst i / a b)
  (cond ((> i (length lst)) lst)
        ((setq a lst b (reverse lst))
         (append (reverse (repeat (- (length b) i) (setq b (cdr b))))
                 (repeat (1+ i) (setq a (cdr a))))
        )
  )
)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
(challenge) nth remover
« Reply #26 on: January 21, 2004, 04:00:41 PM »
Did you notice that it didn't require the setting of any additional variables.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
(challenge) nth remover
« Reply #27 on: January 21, 2004, 05:04:34 PM »
I had an idea too. Now i didnt have much time on this, but here it is in rough form so excuse the mess.

Stig, Remeber that problem we were having with localising var's in a recursive procedure? Heres the answer (And it was soooo simple i cant believe we didnt think of it. :? :lol:)

Ill comment this code later,

Code: [Select]
(defun nthRemover (lst item / nlst cntr)
  (defun Nested-nthRemover (lst item)
    (if (/= (if (not cntr) (setq cntr 0) cntr) item)
      (setq nlst (cons (car lst) nlst)))
    (setq cntr (1+ cntr))
    (if (> (length (cdr lst)) 0)
      (Nested-nthRemover (cdr lst) item)
      (setq cntr nil))
    (reverse nlst))
  (Nested-nthRemover lst item))
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
(challenge) nth remover
« Reply #28 on: January 21, 2004, 05:17:50 PM »
John, do you remember that global variables weren't a problem in recursive functions? Well, you just made them 'global' to the nested function :)

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
(challenge) nth remover
« Reply #29 on: January 21, 2004, 05:31:44 PM »
Oh yeah. Darn! And i thought i was smart there for a second. :lol:
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org