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


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


(quote expression)


  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:

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


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


(eval expression)


  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.


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


(apply function_name list_of_arguments)


  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.


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


(lambda (arguments) …body…)


  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.


Definition: The setq function assigns values to variables.


(setq variable_name value)


  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.


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


(set ‘symbol value)


  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.


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


(boundp ‘variable_name)


  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.


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.