Author Topic: is it possible to make variable "<" and ">" in a while loop ?  (Read 271 times)

0 Members and 1 Guest are viewing this topic.

CatDance

  • Newt
  • Posts: 57
is it possible to make variable "<" and ">" in a while loop ?
« 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)
A must-read book: Amazing story how Bill + Paul started Microsoft and history of computer revolution.
https://www.amazon.com/Hard-Drive-Making-Microsoft-Empire/dp/0887306292

Brief history of Microsoft
https://www.youtube.com/watch?v=BLaMbaVT22E

gile

  • Water Moccasin
  • Posts: 2379
  • Marseille, France
Re: is it possible to make variable "<" and ">" in a while loop ?
« Reply #1 on: January 11, 2022, 02:38:36 AM »
Hi,

With LISP (as with other functional programming laguages) functions are treated as first-class citizens. 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).
Speaking English as a French Frog

gile

  • Water Moccasin
  • Posts: 2379
  • Marseille, France
Re: is it possible to make variable "<" and ">" in a while loop ?
« Reply #2 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. )
Speaking English as a French Frog