AutoLISP: The Complete Guide — Chapter 3 / 16

AutoLISP Syntax

If you have already programmed in a language like Python, JavaScript, or C, LISP syntax may surprise you at first. But don't worry: it is actually simpler and more consistent than most other languages. Once you understand the few basic rules, everything will become clear.

Prefix notation

In most programming languages, operations are written in infix notation: the operator is placed between its operands.

2 + 3

In AutoLISP, we use prefix notation: the operator (or function) comes first, followed by its arguments, all enclosed in parentheses.

(+ 2 3)

This rule is universal in AutoLISP. Whether it is an addition, a function call, or a control structure, the form is always the same:

(function argument1 argument2 ...)

A few examples:

(+ 2 3)           ; Addition: 2 + 3 = 5
(- 10 4)          ; Subtraction: 10 - 4 = 6
(max 5 8 3)       ; Maximum: 8
(strlen "Hello")  ; String length: 5
(alert "Test")    ; Displays a dialog box

The role of parentheses

Parentheses are the heart of LISP. Each pair of parentheses ( and ) delimits a complete expression. An AutoLISP expression is always structured as follows:

( function  argument1  argument2  ...  argumentN )
  ^         ^                          ^
  first     function arguments
  element

The first element after the opening parenthesis is always the function name to call. Everything else constitutes the arguments passed to that function.

No superfluous parentheses

In AutoLISP, every parenthesis has a meaning. Unlike languages where you can add parentheses for readability (such as (2 + 3) in mathematics), in LISP, writing (5) would be an error: AutoLISP would try to call 5 as a function!

(5)   ; ERROR: 5 is not a function
5     ; Correct: simply returns 5

Nested expressions

The true power of this syntax appears when you nest expressions. Since each expression returns a value, you can use it as an argument to another expression:

(+ 2 (* 3 4))

Evaluation proceeds from the inside out:

  1. (* 3 4) -> 12
  2. (+ 2 12) -> 14

A more complex example:

(* (+ 1 2) (- 10 5))
  1. (+ 1 2) -> 3
  2. (- 10 5) -> 5
  3. (* 3 5) -> 15

In standard mathematical notation, this is equivalent to (1 + 2) x (10 - 5).

Evaluation tree of a nested expression

Whitespace and line breaks

AutoLISP is very flexible with whitespace. You can write an expression on a single line:

(+ 2 (* 3 4))

Or on multiple lines for better readability:

(+
  2
  (* 3 4)
)

Both forms are strictly equivalent. Line breaks and multiple spaces are treated as a single separator. Use them to make your code more readable when expressions become complex.

Comments

In AutoLISP, comments start with a semicolon ;. Everything after the semicolon until the end of the line is ignored by the interpreter:

; This is a comment on a full line
(+ 2 3) ; This is an end-of-line comment

For multi-line comments, use ;| and |;:

;| This is a comment
   spanning multiple lines |;

Basic data types

Before going further, here are the data types you will encounter most often in AutoLISP:

Integers

These are numbers without a decimal point:

42
-7
0

Reals (floating-point numbers)

Numbers with a decimal point:

3.14
-0.5
2.0

Strings

Text enclosed in double quotes:

"Hello"
"AutoLISP is awesome"
"Line 1\nLine 2"   ; \n inserts a line break

nil

The value for "nothing", "false", or "no result". It is the equivalent of null, None, or false in other languages:

nil

T

The value for "true":

T

In AutoLISP, any value that is not nil is considered true. T is simply the explicit way of saying "true".

Case sensitivity

AutoLISP is not case-sensitive. The following expressions are all equivalent:

(ALERT "Hello")
(Alert "Hello")
(alert "Hello")

By convention, function names are written in lowercase. However, strings preserve their case: "HELLO" and "hello" are two different strings.

Syntax summary

Rule Example
Prefix notation (+ 2 3) instead of 2 + 3
Parentheses = expression (function args...)
Nesting (+ 2 (* 3 4))
Line comment ; comment
Block comment ;| comment |;
Integer 42
Real 3.14
String "text"
True / False T / nil

Now that you have mastered the basics of the syntax, let's move on to the serious stuff: mathematical operations!


Helping hand Need an AutoCAD (AutoLISP, ObjectARX, .NET, VBA) development? Contact me for a free quote.