I'm new to Lisp. I encountered 2 terms "list" and "S-expression". I just can't distinguish between them. Are they just synonyms in Lisp?
An s-expression, also known as a sexpr or sexp, is a way to represent a nested list of data. It stands for "symbolic expression," and it is commonly encountered in the Lisp programming language and variants of Lisp such as Scheme, Racket, and Clojure.
The syntactic elements of the Lisp programming language are symbolic expressions, also known as s-expressions. Both programs and data are represented as s-expressions: an s-expression may be either an atom or a list. Lisp atoms are the basic syntactic units of the language and include both numbers and symbols.
Lisp has one: everything is a list. The first element is a function name, and the rest are its arguments. Thus, the language is simply a collection of compile- and run-time functions, trivially extensible.
In computer programming, an S-expression (or symbolic expression, abbreviated as sexpr or sexp) is an expression in a like-named notation for nested list (tree-structured) data. S-expressions were invented for and popularized by the programming language Lisp, which uses them for source code as well as data.
First, not all S-expressions represent lists; an expression such as foobar
, representing a bare atom, is also considered an S-expression. As is the "cons cell" syntax, (car . cons)
, used when the "cons" part is not itself another list (or nil). The more familiar list expression, such as (a b c d)
, is just syntactic sugar for a chain of nested cons cells; that example expands to (a . (b . (c . (d . nil))))
.
Second, the term "S-expression" refers to the syntax - (items like this (possibly nested))
. Such an S-expression is the representation in Lisp source code of a list, but it's not technically a list itself. This distinction is the same as that between a sequence of decimal digits and their numeric value, or between a sequence of characters within quotation marks and the resulting string.
That is perhaps an overly technical distinction; programmers routinely refer to literal representations of values as though they were the values themselves. But with Lisp and lists, things get a little trickier because everything in a Lisp program is technically a list.
For example, consider this expression:
(+ 1 2)
The above is a straightforward S-expression which represents a flat list, consisting of the atoms +
, 1
, and 2
.
However, within a Lisp program, such a list will be interpreted as a call to the +
function with 1 and 2 as arguments. (Do note that is the list, not the S-expression, that is so interpreted; the evaluator is handed lists that have been pre-parsed by the reader, not source code text.)
So while the above S-expression represents a list, it would only rarely be referred to as a "list" in the context of a Lisp program. Unless discussing macros, or the inner workings of the reader, or engaged in a metasyntactic discussion because of some other code-generation or parsing context, a typical Lisp programmer would instead treat the above as a numeric expression.
On the other hand, any of the following S-expressions likely would be referred to as "lists", because evaluating them as Lisp code would produce the list represented by the above literal S-expression as a runtime value:
'(+ 1 2) (quote (+ 1 2)) (list '+ 1 2)
Of course, the equivalence of code and data is one of the cool things about Lisp, so the distinction is fluid. But my point is that while all of the above are S-expressions and lists, only some would be referred to as "lists" in casual Lisp-speak.
S-expressions are a notation for data.
Historically an s-expression (short for symbolic expression) is described as:
FOO
and BAR
(
expression-1 .
expression-2 )
NIL
(
A
.
(
B
.
NIL
)
)
is simpler written as the list (A B)
Note also that historically program text was written differently. An example for the function ASSOC
.
assoc[x;y] = eq[caar[y];x] -> cadar[y]; T -> assoc[x;cdr[y]]
Historically there existed also a mapping from these m-expressions (short for meta expressions) to s-expressions. Today most Lisp program code is written using s-expressions.
This is described here: McCarthy, Recursive Functions of Symbolic Expressions
In a Lisp programming language like Common Lisp nowadays s-expressions have more syntax and can encode more data types:
symbol123
, |This is a symbol with spaces|
123
, 1.0
, 1/3
, ..."This is a string"
#\a
, #\space
#(a b c)
( a . b )
, (a b c)
; this is a comment
, #| this is a comment |#
and more.
Lists
A list is a data structure. It consists of cons cells and a list end marker. Lists have in Lisp a notation as lists in s-expressions. You could use some other notations for lists, but in Lisp one has settled on the s-expression syntax to write them.
Side note: programs and forms
In a programming language like Common Lisp, the expressions of the programming language are not text, but data! This is different from many other programming languages. Expressions in the programming language Common Lisp are called Lisp forms
.
For example a function call is Lisp data, where the call is a list with a function symbol as its first element and the next elements are its arguments.
We can write that as (sin 3.0)
. But it really is data. Data we can also construct.
The function to evaluate Lisp forms is called EVAL
and it takes Lisp data, not program text or strings of program text. Thus you can construct programs using Lisp functions which return Lisp data: (EVAL (LIST 'SIN 3.0))
evaluates to 0.14112
.
Since Lisp forms have a data representation, they are usually written using the external data representation of Lisp - which is what? - s-expressions!
It is s-expressions. Lisp forms as Lisp data are written externally as s-expression.
You should first understand main Lisp feature - program can be manipulated as data. Unlike other languages (like C or Java), where you write program by using special syntax ({
, }
, class
, define
, etc.), in Lisp you write code as (nested) lists (btw, this allows to express abstract syntactic trees directly). Once again: you write programs that look just like language's data structures.
When you talk about it as data, you call it "list", but when you talk about program code, you should better use term "s-expression". Thus, technically they are similar, but used in different contexts. The only real place where these terms are mixed is meta-programming (normally with macros).
Also note that s-expression may also consist of the only atom (like numbers, strings, etc.).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With