Introduction
Welcome to a new module, "Variables and Core Data Types." In the previous lessons, we explored the foundational paradigm of Lisp: S-expressions, homoiconicity ("code as data"), and the mechanisms of quote and eval. You learned how to dynamically construct a list and then execute it as code.
Now, we shift our focus from the structure of code to its substance. For an expression like (+ 10 20) to be useful, we need to understand what +, 10, and 20 actually are. They are the fundamental building blocks of our programs: data.
In this lesson, your learning outcome is to identify and use Elisp's core data types: integers, floats, strings, symbols, and booleans (t and nil). We will take a systematic tour of these types, learning how to recognize them, create them, and check what type a piece of data is. This is the first step toward storing, manipulating, and making decisions based on data in your own Emacs functions.
1. What is a Data Type?
Since you've requested we cover foundational concepts, let's start with a definition. A data type is a classification that tells the computer how to interpret a value. It specifies what kind of data an object can hold (e.g., a whole number, a piece of text) and what operations can be performed on it (e.g., you can do arithmetic on numbers, but not on text).
Elisp, like any programming language, has a set of built-in data types. We'll start with the most familiar ones.
2. Numbers: Integers and Floats
Numbers in Elisp are straightforward. They come in two primary forms:
- Integers: Whole numbers, like
1,42,-100. - Floating-point numbers (Floats): Numbers with a decimal part, like
3.14,-0.5,2.0.
When you evaluate a number in Elisp, it simply returns itself. This makes them "self-evaluating" types.
Checking the Type: Predicates
A common task in programming is to check if a value is of a certain type. In Elisp, this is done with functions called predicates. A predicate is a function that answers a yes/no question, returning t (true) or nil (false). By convention, predicate functions in Elisp often end with the letter p.
integerpchecks if a value is an integer.floatpchecks if a value is a float.numberpchecks if a value is any kind of number.
Types, Conditionals, and Loops - Learning Emacs Lisp #2
The video 'Types, Conditionals, and Loops' by System Crafters provides an excellent overview of Elisp's number types. It will show you how they are written and how to perform basic operations and checks on them.
Please watch the segment from 10:32 to 13:48, which covers integers and floats. Then, continue from 17:40 to 20:18, where the presenter discusses the corresponding predicates (integerp, floatp, numberp).
Hands-on Practice
Open your *scratch* buffer and try evaluating these expressions yourself by placing your cursor after each one and pressing C-x C-e. Observe the result in the minibuffer.
42 ; An integer
-3.14 ; A float
(integerp 42) ; Returns t
(integerp 42.0) ; Returns nil, because it's a float
(floatp 42.0) ; Returns t
(numberp 42) ; Returns t
(numberp 42.0) ; Returns t
3. Strings
A string is a sequence of characters, like a word, a sentence, or a paragraph. In Elisp, strings are enclosed in double quotes ("). Like numbers, strings are self-evaluating.
"Hello, world!"
"This is a string."
"" ; An empty string
The predicate for checking if a value is a string is stringp.
As a writer, you can imagine how central strings are. Any function you write to automate tasks related to your creative writing—manipulating sentences, searching for patterns, inserting boilerplate text—will rely heavily on string manipulation.
Types, Conditionals, and Loops - Learning Emacs Lisp #2
The same System Crafters video also has a thorough section on strings. It covers their creation, the stringp predicate, and other useful operations that we will revisit later in the course.
Watch the segment on strings from 31:44 to 36:30. Pay close attention to how strings are defined and how predicates like stringp and string-empty-p are used.
Hands-on Practice
Try these in your *scratch* buffer:
"speculative fiction" ; A string
(stringp "Elisp") ; Returns t
(stringp 42) ; Returns nil
4. Booleans: The Nature of Truth in Lisp
In most programming languages, you have specific "boolean" values for true and false. Elisp is a bit different, and this is a key aspect of the Lisp philosophy.
- False is represented by the special symbol
nil. - True is represented by the special symbol
t. More broadly, in a logical context, any value other thannilis considered true.
This means 0, the empty string "", and any other value will be treated as true in a conditional check. Only nil is false.
Crucially, t and nil are not a special "boolean" type. They are just symbols. The symbol nil has a unique triple role: it means "false," it is a symbol named "nil," and it also represents the empty list '(). This is a powerful and elegant design choice you'll come to appreciate.
Types, Conditionals, and Loops - Learning Emacs Lisp #2
Let's turn back to the System Crafters video for a concise explanation of t and nil.
First, watch from 03:49 to 04:55 to understand that t and nil are symbols. Then, watch from 55:15 to 56:55, where the concept of 'truthiness' (what Elisp considers true or false) is explained.
The primary predicate related to nil is null, which returns t if its argument is nil, and nil otherwise.
(null nil) ; Returns t
(null '()) ; Also returns t, because nil is the empty list
(null 0) ; Returns nil
(null "hello") ; Returns nil
5. Symbols: The Heart of Lisp's Variables and Functions
We've arrived at the most distinctively Lisp-like data type: the symbol. You've already seen symbols everywhere: +, message, integerp, t, and nil are all symbols.
Protesilaos Stavrou's article 'Emacs Lisp Elements' provides a clear, high-level definition of what a symbol is.
Please read the first three paragraphs in the section '2. Basics of how Lisp works'. Focus on the distinction made between strings (text in double quotes) and symbols (words not in quotes that 'symbolise' something).
As the article explains, the key difference is:
- A string is just a piece of text. The string
"message"is its own value. - A symbol is a name that refers to a value. The symbol
messagerefers to the built-in function that prints messages.
Except for the special cases of t and nil, symbols are not self-evaluating. When Elisp evaluates a symbol, it looks up the value associated with it. This is how variables work.
If you try to evaluate a symbol that has no value, you'll get an error:
my-undefined-symbol ; C-x C-e here will produce a "void variable" error
To refer to the symbol itself rather than its value, you must quote it with a single quote (').
'my-undefined-symbol ; This evaluates to the symbol my-undefined-symbol itself
The predicate for this type is symbolp.
Hands-on Practice
This is a critical distinction to grasp. Evaluate the following in your *scratch* buffer.
(stringp "message") ; t, because "message" is a string
(symbolp "message") ; nil
(symbolp 'message) ; t, because 'message is a symbol
(stringp 'message) ; nil
This shows that even though they look similar, a string and a symbol are fundamentally different types of data.

Conclusion
In this lesson, we've surveyed the five most fundamental data types in Emacs Lisp. Understanding these is essential for writing any Elisp code.
Key Takeaways:
- Integers and Floats are self-evaluating number types. Use
integerp,floatp, andnumberpto check them. - Strings are self-evaluating sequences of characters in double quotes. Use
stringpto check for them. - Booleans in Elisp are not a distinct type. The symbol
nilrepresents false, and any other value (including the symbolt) represents true. - Symbols are names that refer to other values (like variables or functions). They are not self-evaluating and must be quoted (
') to refer to the symbol itself. Usesymbolpto check for them.
Preview of the Next Lesson:
You now know what a symbol is and that it acts as a name referring to a value. But how do you create that association? In our next lesson, we will learn how to assign values to global variables using setq, officially starting our journey into creating and managing state in our programs.