TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: CatDance on January 11, 2022, 01:13:06 AM

Title: is it possible to make variable "<" and ">" in a while loop ?
Post by: CatDance on January 11, 2022, 01:13:06 AM
example ..
Code - Auto/Visual Lisp: [Select]
  1. (defun subr_A ( SignOf<and> angle / )
  2.   (while (SignOf<and>  angle 90)    ;eg (while (< 45 90)
  3.     .... then do stuff
  4.    
  5.  
  6.   );while
  7. );defun
  8.  

SignOf<and> is a var of < or > depending on what I set it before going into this subr to do stuff

Example (Subr_A < 45)
Title: Re: is it possible to make variable "<" and ">" in a while loop ?
Post by: gile on January 11, 2022, 02:38:36 AM
Hi,

With LISP (as with other functional programming (https://www.tutorialspoint.com/functional_programming/functional_programming_introduction.htm) laguages) functions are treated as first-class citizens (https://en.wikipedia.org/wiki/First-class_citizen). That means functions can be used the same way as other data type, e.g. stored in variables, passed as arguments to other functions, returned by functions.

In you case, you can directly pass the < or > operator to the subr_A function:
Code - Auto/Visual Lisp: [Select]
  1. (subr_A < 45)
or store it in a variable before:
Code - Auto/Visual Lisp: [Select]
  1. (setq op <)
  2. (subr_A op 45)

But, to be consistent with native higher order functions, you should quote the function passed as argument.
Code - Auto/Visual Lisp: [Select]
  1. (defun subr_A (SignOf<and> ang)
  2.   (while (apply SignOf<and> (list ang 90))
  3.     .... then do stuff
  4.   )
  5. )
Then you can call it with the quoted operator
Code - Auto/Visual Lisp: [Select]
  1. (subr_A '< 45)
or store it in a variable before:
Code - Auto/Visual Lisp: [Select]
  1. (setq op '<)
  2. (subr_A op 45)

NOTA: you should not use 'angle' as variable name. It is a protected symbol (native function).
Title: Re: is it possible to make variable "<" and ">" in a while loop ?
Post by: gile on January 11, 2022, 02:43:26 AM
Another way of defining your 'subr_A' function so that it can accept either quoted or non-quoted operator:
Code - Auto/Visual Lisp: [Select]
  1. (defun subr_A (SignOf<and> ang)
  2.   (setq SignOf<and> (eval SignOf<and>))
  3.   (while (SignOf<and> ang 90)
  4.     .... then do stuff
  5.   )
  6. )