Catalog / Lisp Programming Cheatsheet
Lisp Programming Cheatsheet
A concise reference for Lisp programming, covering syntax, data structures, functions, macros, and common operations. Useful for both beginners and experienced Lispers.
Basic Syntax & Data Types
Syntax Fundamentals
Lisp syntax is based on s-expressions (symbolic expressions). |
|
Parentheses are crucial. They define the structure and order of operations. |
Comments start with a semicolon |
Basic Data Types
Numbers: |
Integers (e.g., |
Symbols: |
Represent variables, function names, etc. (e.g., |
Strings: |
Sequences of characters enclosed in double quotes (e.g., |
Characters: |
Represented differently depending on the Lisp dialect. (e.g., |
Booleans: |
|
Lists: |
Ordered collections of elements enclosed in parentheses (e.g., |
Lists and Cons Cells
Lists are built from cons cells. A cons cell holds two pointers: |
|
|
|
Functions and Control Flow
Defining Functions
|
Example:
|
Parameters are symbols that receive the argument values when the function is called. |
Control Flow
|
|
|
|
|
A conditional that compares a key against multiple values.
|
|
Powerful iteration construct with many clauses for different looping behaviors. Too complex to summarize here, but essential for serious Lisp programming. |
Lambda Functions
|
Lambda functions are often used as arguments to other functions (higher-order functions). |
|
|
Variables and Scope
Variable Binding
|
|
|
Scope
Lisp typically uses lexical (static) scoping. Variables are visible within the block they are defined and any nested blocks, unless shadowed by a new binding. |
Global variables can be defined using |
|
Data Structures
Arrays |
Arrays can be created using
|
Hash Tables |
Hash tables store key-value pairs. Created with
|
Structures |
User-defined data types with named slots. Defined using
|
Macros
Macro Definition
Macros are code that write code. They are expanded at compile time. |
Example:
This macro defines a short-circuiting ‘or’ operator. |
Quoting and Unquoting
|
Prevents evaluation. Returns the expression literally.
|
`` (backquote)` |
Similar to
|
|
Inside a backquote, evaluates the expression and splices the result.
|
,@ |
Used inside a backquote to splice a list into the surrounding list.
|
Macro Expansion
|
Understanding macro expansion is crucial for debugging and understanding macro behavior. |