Author Topic: Use Operator from Variable  (Read 1564 times)

0 Members and 1 Guest are viewing this topic.

mailmaverick

  • Bull Frog
  • Posts: 494
Use Operator from Variable
« on: February 15, 2017, 08:30:03 AM »
Is it possible to give + or - operators through variables ?
Such as
(setq op '+)
(op 5 2)
Result = 7

(setq op '-)
(op 5 2)
Result = 3




mailmaverick

  • Bull Frog
  • Posts: 494
Re: Use Operator from Variable
« Reply #1 on: February 15, 2017, 08:42:15 AM »
Found one solution using apply :
(setq op '+)
(apply op (list 5 2))
Result = 7

(setq op '-)
(apply op (list 5 2))
Result = 3

Is there any other solution ?



Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Use Operator from Variable
« Reply #2 on: February 15, 2017, 08:47:41 AM »
To shed some light:
Code: [Select]
_$ (type +)
SUBR
_$ (type '+)
SYM

_$ (setq op +)
#<SUBR @000000097244fba8 +>
_$ (op 5 2)
7
_$ (eval '(op 5 2))
7

_$ (setq op '+)
+
_$ (apply op '(5 2))
7
EDIT: Another way would be to use the read function, so this thread is somewhat related.
« Last Edit: February 15, 2017, 08:51:43 AM by Grrr1337 »
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Use Operator from Variable
« Reply #3 on: February 15, 2017, 08:52:03 AM »
Both those would work, though only with a few tweaks. It's one of the features of Lisp in general. There's no difference between an "operator" and a function, in Lisp (all Lisp's not just AutoLisp) everything is a function or a value, and both can be assigned to a symbol.

Code - Auto/Visual Lisp: [Select]
  1. (setq op +)
  2. (op 5 2)
  3. ;; Result = 7

Or if you quote the "operator", then you need to "de-quote" it when you use it - i.e. use something like apply which takes a quoted symbol of the function instead of just the function. Thus:
Code - Auto/Visual Lisp: [Select]
  1. (setq op '+)
  2. (apply op '(5 2))
  3. ;; Result = 7
« Last Edit: February 15, 2017, 09:57:55 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

mailmaverick

  • Bull Frog
  • Posts: 494
Re: Use Operator from Variable
« Reply #4 on: February 15, 2017, 09:53:47 AM »
Thanks a lot. My query is resolved.

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Use Operator from Variable
« Reply #5 on: February 15, 2017, 10:14:38 AM »
I've done that in the past.  I needed a means to mass-convert drawing content in terms of layers, line types, color, and so on.  Naturally the drawings could not be counted on to even be consistently wrong.  So I built an expert system in LISP, using (apply ...) frequently to apply both math and logic rules.  Worked quite well except rules had to be constructed manually which was time consuming.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}