Author Topic: Tutorial 2 : AutoLisp : Clean up after yourself  (Read 1555 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Tutorial 2 : AutoLisp : Clean up after yourself
« on: January 22, 2020, 04:21:55 PM »
Localize Variables
Locaizing Variables means to essentially set their value to nil (nothing, zero, empty). This can be done one-by-one by using the SETQ function or with a simple shortcut (everyone uses the shortcut). If take a close look at the first line of our SquareRoot code you'll see the shortcut.

(defun SquareRoot ( x / squareroot-iter good-enough? improve average )

Everything listed after the forward slash will be localized [i.e. only available to this function, and otherwise nil]. In this case I am localizing the helper functions but you can also localize variables you create/assign/define in your code.

Garbage Collecting
Memory Management is a very big, and involved, topic and we will not be diving too deep into the subject--in fact, I will only be giving a very simplified overview of the subject. You can think of memory like an ice-cube tray (a metaphor). An ice-cube tray only has so many slots; as a program is run it fills up slots (creates variables, for example) and when all of the slots are filled the program crashes. When you localize--or set a variable to nil--you are essentially emptying a slot, which can be filled again. For example:

Code - Auto/Visual Lisp: [Select]
  1. (setq a 1)
  2. ; Create/define a variable "A" and assign it the value of "1"
  3. (setq a nil)
  4. ; Empty that slot [-i.e. destroy that variable definition]

Once that variable is destroyed, that variable cannot be used by your program.

Types
There are several different types of..."things" you can assign to a variable; Numbers, Text, Lists, Boolean, and etc. And there are several different types of types. Meaning that you have different types of numbers. For example: the number "1" and the number "1.0" are different. For the most part, you won't have to concern yourself with the different types of numbers/etc. until you start preforming "precise mathematics on numbers in certain situations" (don't worry about what that means right now) but you will have to concern yourself with the differences between a number and a boolean value assigned to a variable.

Code - Auto/Visual Lisp: [Select]
  1. (setq a 1)
  2. ; Create/define a variable "A" and assign it the number value of "1"
  3. (setq b T)
  4. ; Create/define a variable "B" and assign it the boolean value of TRUE

TIP: did you notice the additional language I used in the description ("...assign number/boolean value...")?

Logic Primer
We can preform simple logic based decisions in programming and that is done with simple logic based functions. "IF" is one logic type function and it's syntax is:
(if <test> <then> [else])
Pay attention to the brackets in the above, the angle brackets are required arguments and the square brackets are optional arguments.

Code - Auto/Visual Lisp: [Select]
  1. (setq a T)
  2. ; Create/define a variable "A" and assign it the boolean value of "TRUE"
  3. (if a "YES!")
  4. ; Logic: if the value of "A" is true then return "YES!".

You may not realize it but I just touched on a subject that will plague and problem you for your entire programming career. But don't worry, many, many programmers do not get this subject correct and I'll do my best to layout some of the more important points for you to pay attention to. But, the question you may be asking yourself right away is where does "YES!" return to?

The answer to that is at the crux of the future problems you will find yourself in but for now let us just say it is returned to memory.

The help documentation for the IF function basically says:
IF will evaluate the THEN expression if the TEST expression is not nil (notice that they did not say "true" they said "not nil").

Depending on the expressions within the IF statement, it can return a multitude of things (can be a number, string, boolean, etc.) depending on how you use it. That is just a long-about-way of saying you need to be mindful of what type of thing your statement returns.

In AutoLisp we essentially string several commands together and each statement's return is input for the next statement.

Returns
I've told you that each statement, function, or block of statements has a return type but that is a pretty vague statement/concept for you right now. Imagine you string a series of commands together, each return is the input to the next statement.

(function4 (function3 (function2 (function1))))

If the return from one of those functions is of a different type then the next function expects, CRASH!

To give you an example:
One of the lines in the AVERAGE function, in the SquareRoot example, was:
(/ (apply '+ args) (length args))

If the LENGTH or APPLY functions had the possibility to return something other then a number the DIVIDE operation could possibly fail and I would have to account for that.

Return types is a big topic, and as I said above this will give you problems throughout your time programming so I will leave further examples and discussions for later.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org