Learn the basics of AutoCAD and More!

Lesson 5.04 – Understanding LISP Syntax

🧬 Lesson 5.04 – Understanding LISP Syntax

Master the building blocks of AutoLISP: atoms, lists, functions, and how evaluation works.


📚 What You’ll Learn

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

  • Understand what atoms and lists are in LISP
  • Write simple AutoLISP expressions using correct syntax
  • Grasp how AutoLISP evaluates expressions using parentheses
  • Read and debug basic AutoLISP code confidently

🧠 Why It Matters

AutoLISP is a list-based language where everything is an expression. Once you understand how LISP reads and evaluates its code, writing and debugging scripts becomes easier and more predictable. This lesson is essential for building confidence in writing your own routines.


🛠️ Tools You’ll Use

Tool / FeaturePurpose
LISP ExpressionsLearn the basic structure of all AutoLISP code
Command LineTest expressions and evaluate results
PRINT, PRINCOutput results of expressions
QUOTEPrevent evaluation to view data as-is

🧭 Lesson Structure

1️⃣ What Is an Atom?

An atom is a single element in LISP. It can be:

TypeExampleDescription
Number5, -2.75A numeric value
Symbolx, pt1Variable or function name
String"Hello"Text enclosed in quotes
BooleanT, NILT = true, NIL = false or empty list

Atoms are the smallest units of information in AutoLISP.

2️⃣ What Is a List?

A list is a group of items enclosed in parentheses.
In AutoLISP, all code is written in list form:

(+ 2 3)   ; This is a list with function "+" and two arguments
ExampleMeaning
(+ 2 3)Add 2 and 3 → returns 5
(* 4 2)Multiply 4 and 2 → returns 8
(setq x 10)Assign value 10 to variable x

📌 Lists are always written in prefix notation:

(operator operand1 operand2 ...)

3️⃣ Parentheses and Evaluation

AutoLISP evaluates the first element in a list as a function or operator.

(+ 2 2)     ; "+" is the function, 2 and 2 are the arguments
(setq a 5) ; "setq" sets variable a to 5
RuleExample
First item = function(+ 1 2) → returns 3
Inner lists are evaluated first (nested)(+ (* 2 2) 3) → 7
Quotes prevent evaluation'(1 2 3) → returns list
(setq pt '(10 20)) ; pt is a list containing 10 and 20

4️⃣ Common Built-In Functions

FunctionPurposeExampleResult
+Add numbers(+ 2 3)5
*Multiply numbers(* 4 2)8
setqAssign a value to a variable(setq x 10)10
listCombine atoms into a list(list 1 2 3)(1 2 3)
quotePrevent evaluation(quote (a b))(a b)

Lesson Checklist

TaskCompleted
Identify atoms and lists in sample code
Write a basic list using prefix notation
Use setq to assign a value
Understand how parentheses affect evaluation
Use quote to treat a list as data, not code

📌 Quick Tips

TipWhy It Helps
Always count your parenthesesMismatched parentheses cause most LISP errors
Use semicolons ; to add commentsMakes code readable and maintainable
Avoid redefining built-in symbolsUsing list, setq as variable names is risky
Test small code snippets in the command lineHelps confirm understanding of expressions

🧩 Real-World Applications

ScenarioExample
Drawing input storageStore user-picked points in a list
Geometry creationUse command with coordinates
Looping over points or objectsLists become the basis for iteration

📁 Files and Resources

File / ResourceDescription
syntax_practice.lspSample script with list/atom examples
prefix_cheatsheet.pdfPrintable guide to prefix notation
evaluation_examples.txtBefore/after examples of evaluated expressions

📖 Review Table

ConceptMeaningExample
AtomA single data element5, "text", x
ListA group of elements in parentheses(+ 1 2)
Prefix FormOperator comes before operands(* 3 4)
EvaluationFirst element is treated as a function(+ 3 2) = 5
QuotePrevents evaluation'(a b c) = (a b c)