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)