Lessons

Advanced Programming & Control Flow

As you continue your journey in the AutoLISP realm, it’s time to venture into Advanced Programming & Control Flow. While the basics gave you a strong foundation, mastering advanced concepts will unlock a world of sophisticated programming techniques in CAD design and beyond.


Learning Outcomes

By the end of this lesson, you should be able to:

  • Understand and use advanced Lisp functions for better code structure and logic.
  • Differentiate between literal data and its evaluation using quote.
  • Manipulate data dynamically using eval and apply.
  • Define anonymous functions with lambda.
  • Assign values to variables using setq and set.
  • Check variable existence using boundp.

Diving Deeper into Control Flow

quote

Definition: The quote function prevents evaluation and returns the argument as a literal expression.

Syntax:

(quote expression)

Examples:

  1. Quoting a list:
(quote (1 2 3))

Returns the list (1 2 3) without evaluating it.

  1. Quoting a symbol:
(quote x)

Returns the symbol x.

  1. Using the shorthand ‘ for quote:
‘x

This is equivalent to (quote x) and returns the symbol x.


eval

Definition: The eval function evaluates an expression and returns its value.

Syntax:

(eval expression)

Examples:

  1. Evaluating a mathematical operation:
(eval ‘(+ 1 2))

Returns 3.

  1. Evaluating a list as a function call:
(setq x ‘(+ 3 4))
(eval x)

This returns 7.

  1. Evaluating nested expressions:
(eval ‘(+ (* 2 3) (- 5 2)))

Returns 9.


apply

Definition: The apply function evaluates a function with arguments provided in a list.

Syntax:

(apply function_name list_of_arguments)

Examples:

  1. Applying a function to a list of arguments:
(apply ‘+ ‘(1 2 3))

Returns 6.

  1. Using apply with user-defined functions:
(defun add (x y) (+ x y))
(apply ‘add ‘(4 5))

Returns 9.

  1. Applying a function to multiple arguments:
(apply ‘* 2 3 ‘(4))

Returns 24.


lambda

Definition: The lambda function creates an anonymous function (a function without a name).

Syntax:

(lambda (arguments) …body…)

Examples:

  1. Basic lambda function:
((lambda (x) (* x x)) 5)

Returns 25.

  1. Using lambda with apply:
(apply (lambda (x y) (+ x y)) ‘(6 7))

Returns 13.


setq

Definition: The setq function assigns values to variables.

Syntax:

(setq variable_name value)

Examples:

  1. Assigning a number to a variable:
(setq a 10)

This assigns the value 10 to the variable a.

  1. Assigning a list to a variable:
(setq myList ‘(1 2 3))

Now, myList holds the list (1 2 3).

  1. Assigning multiple variables at once:
(setq x 5 y 6 z 7)

Assigns 5 to x, 6 to y, and 7 to z.


set

Definition: The set function assigns a value to a symbol, which is evaluated.

Syntax:

(set ‘symbol value)

Examples:

  1. Assigning a value to a symbol:
(set ‘b 20)

This assigns the value 20 to the symbol b.

  1. Assigning a list to a symbol:
(set ‘myList2 ‘(4 5 6))

The symbol myList2 now refers to the list (4 5 6).

  1. Reassigning a variable’s value:
(setq c 30)
(set ‘c 35)

The value of c is updated to 35.


boundp

Definition: The boundp function checks if a variable is bound to a value.

Syntax:

(boundp ‘variable_name)

Examples:

  1. Checking a bound variable:
(setq d 40)
(boundp ‘d)

Returns T since d is bound to a value.

  1. Checking an unbound variable:
(boundp ‘e)

Returns nil as e is not bound to any value.

  1. Rechecking after assigning a value:
(setq e 50)
(boundp ‘e)

Returns T.


Conclusion

Mastering Advanced Programming & Control Flow is crucial for creating complex routines and automations in AutoLISP. These advanced techniques allow for dynamic data manipulation and fine-grained control over program execution. By practicing and experimenting with these functions, you’ll greatly enhance your capability to tackle intricate problems in CAD design using AutoLISP. Always remember: practice is the key to mastery! Keep coding and exploring the depth of AutoLISP functionalities.