Author Topic: Perform operation to a single item in list  (Read 4217 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: Perform operation to a single item in list
« Reply #15 on: July 16, 2009, 05:07:46 PM »
Michael,

I see you use that "Double Defun" trick on this code too:

http://www.theswamp.org/index.php?topic=4903.0  (Great code btw)

But I am having real trouble understanding how it works/is evaluated...

If you have a bit of time, would you be able to shed some light for my confused mind...  :-)

Thanks,

Lee

EDIT: just seen you have an explanation on a few pages later in that thread... never mind - I must be blind

JohnK

  • Administrator
  • Seagull
  • Posts: 10655
Re: Perform operation to a single item in list
« Reply #16 on: July 16, 2009, 05:10:01 PM »
Quote
...(if (eq n (setq i (+ i j)))
            (progn (defun bar (a) a) (foo a))
            a
        )
hehehe ...nice. I could swear, i had used a redefinition (a quine is an exact reproduction but what would this be called?) recently but i cant for the life of me remember where or why.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Perform operation to a single item in list
« Reply #17 on: July 16, 2009, 05:12:17 PM »
Michael, you code needs some fixing

(ApplyFooToNth (lambda (x) (+ 7 x)) 0 '(1 2 3 4 5))

=> (1 2 3 4 12)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Perform operation to a single item in list
« Reply #18 on: July 16, 2009, 05:23:54 PM »
You're right vovka, juggling too much here and I combined 2 different approaches. Should have tried it before posting it. :facepalm:

Here's a quick alternate:

Code: [Select]
(defun ApplyFooToNth ( foo n lst / bar )

    (defun bar ( a )
        (if (eq n (setq i (1+ i)))
            (progn (defun bar (a) a) (foo a))
            a
        )
    )
   
    ((lambda (i) (mapcar 'bar lst)) -1)
   
)

(ApplyFooToNth (lambda (x) (+ 7 x)) 0 '(1 2 3 4 5)) => (8 2 3 4 5)

(ApplyFooToNth (lambda (x) (+ 7 x)) 1 '(1 2 3 4 5)) => (1 9 3 4 5)

(ApplyFooToNth (lambda (x) (+ 7 x)) 2 '(1 2 3 4 5)) => (1 2 10 4 5)

(ApplyFooToNth (lambda (x) (+ 7 x)) 3 '(1 2 3 4 5)) => (1 2 3 11 5)

(ApplyFooToNth (lambda (x) (+ 7 x)) 4 '(1 2 3 4 5)) => (1 2 3 4 12)

My apologies peeps and thank you vovka. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Perform operation to a single item in list
« Reply #19 on: July 16, 2009, 05:32:49 PM »
my version...
Code: [Select]
(defun f (a b c)
 (cond ((not c) c)
       ((> b 0) (cons (car c) (f a (1- b) (cdr c))))
       (t (cons (a (car c)) (cdr c)))
 ) ;_  cond
)
test:
Code: [Select]
(f (lambda (x) (+ 7 x)) 0 '(1 2 3 4 5)) => (8 2 3 4 5)
(f (lambda (x) (+ 7 x)) 2 '(1 2 3 4 5)) => (1 2 10 4 5)
(f (lambda (x) (+ 7 x)) 5 '(1 2 3 4 5)) => (1 2 3 4 5)

Binky

  • Guest
Re: Perform operation to a single item in list
« Reply #20 on: July 16, 2009, 10:56:50 PM »
OK here is what I think I will end up with, stealing a bit from here and another bit from there.

CAB that was short, sweet and to the point.  Nice!

MP, It took me a minute to see the value of redefining the function once the particular index was reached(never would have thought of that myself, thanks for the enlightenment), as it saves time from having to re-evaluate the test condition after the need for it no longer exists.  That would save time on large lists.

Tim makes a great point about ordering the arguments for readability, I often forget that.

Code: [Select]
(defun nthadjust (opr val pos lst / foo)
  (defun foo (x)
    (if (= pos (setq i (1+ i)))
      (progn
        (defun foo (x) x)
        (opr val x))
      x))

  ((lambda (i) (mapcar 'foo lst)) -1)
)

Thanks everybody

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Perform operation to a single item in list
« Reply #21 on: July 17, 2009, 12:05:56 AM »
OK here is what I think I will end up with, stealing a bit from here and another bit from there.

...


that is the point ... we supply various bits of information that you can decide whether it is useful or not, take the good bits, toss the rest ...

We like to think of it as teaching a man to fish
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