Author Topic: need short formula  (Read 2490 times)

0 Members and 1 Guest are viewing this topic.

Adesu

  • Guest
need short formula
« on: March 31, 2010, 02:54:52 AM »
Would you please help me to find "short formula" or not long, in order variable xyz + lst -> '(30 35 40).
I just create a code but still wrong

Code: [Select]
(setq xyz '(10 20 20))
(setq lst '(20 15 20))
it should become (setq xyz '(30 35 40))

   
(foreach item xyz
  (setq x (+ item (car lst)))
  (setq y (+ item (cadr lst)))
  (setq z (+ item (caddr lst)))
  ) ; foreach
(princ x)
(princ y)
(princ z)

pkohut

  • Guest
Re: need short formula
« Reply #1 on: March 31, 2010, 03:24:16 AM »
Code: [Select]
(mapcar '+ xyz lst)
Code: [Select]
(setq op (mapcar '+ xyz lst)
      x (car op)
      y (cadr op)
      z (caddr op))
« Last Edit: March 31, 2010, 03:28:49 AM by pkohut »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: need short formula
« Reply #2 on: March 31, 2010, 09:22:32 AM »
I'm not sure I understand the question at all, but maybe you'll find this useful / interesting:

Assuming lst is set to (1 2 3) ...

(mapcar 'set '(x y z) lst)

Will bind 1 to x, 2 to y, 3 to z.

And assuming lst1 is set to (1 2 3) and lst2 is set to (4 5 6) ...

(mapcar '+ lst1 lst2)

Will return (5 7 9)

Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Adesu

  • Guest
Re: need short formula
« Reply #3 on: March 31, 2010, 09:41:27 AM »
Wow......it's code I look for, thanks

Code: [Select]
(mapcar '+ xyz lst)

Adesu

  • Guest
Re: need short formula
« Reply #4 on: March 31, 2010, 09:45:07 AM »
Yes this I look for,thanks.

(mapcar '+ lst1 lst2)



alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: need short formula
« Reply #5 on: March 31, 2010, 09:46:47 AM »
Code: [Select]
(mapcar (function (lambda (a b v) (set v (+ a b))))
        xyz lst
        '(x y z)
        )
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

uncoolperson

  • Guest
Re: need short formula
« Reply #6 on: March 31, 2010, 11:38:16 AM »
Code: [Select]
(DEFUN plus (v1 v2) (MAPCAR (FUNCTION +) v1 v2))
more of the same, pretty sure I stole this from someone here.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: need short formula
« Reply #7 on: April 01, 2010, 05:51:13 AM »
Another   :lol:

Code: [Select]
(  (lambda (a b)
       
        (  (lambda (f i) (apply f i))
           (function mapcar)
           (  (lambda (f i) (cons f i))
              (function set)
              (  (lambda (f i) (cons f i))
                 (quote (x y z))
                 (  (lambda (f i) (f i))
                    (lambda (i) (list i))
                    (  (lambda (f i) (apply f i))
                       (function mapcar)
                       (  (lambda (f i) (cons f i))
                          (function +)
                          (list a b)
                       )
                    )
                 )
              )
           )
        )
   )
   xyz lst
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: need short formula
« Reply #8 on: April 01, 2010, 12:32:46 PM »
my way:
Code: [Select]
(mapcar 'set '(x y z) (mapcar '+ xyz lst))

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: need short formula
« Reply #9 on: April 01, 2010, 12:46:59 PM »
my way:
Code: [Select]
(mapcar 'set '(x y z) (mapcar '+ xyz lst))
Nice and simple. I like it.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox