ArticleslgStudy

science

S-expression

S-expression is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand S-expression rather than just read about it. In short: 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.

S-expression — main illustration
S-expression — illustration

Key takeaways

  • S-expression belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect S-expression to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of S-expression from memory before moving on to harder problems.

Reference excerpt

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.

Characteristics In the usual parenthesized syntax of Lisp, an S-expression is classically defined as

an atom of the form x, or an expression of the form (x . y) where x and y are S-expressions. This definition reflects LISP's representation of a list as a series of "cells", each one an ordered pair. In plain lists, y points to the next cell (if any), thus forming a list. The recursive clause of the definition means that both this representation and the S-expression notation can represent any binary tree. However, the representation can in principle allow circular references, in which case the structure is not a tree at all, but a cyclic graph, and cannot be represented in classical S-expression notation unless a convention for cross-reference is provided, analogous to SQL foreign keys, SGML/XML IDREFs, etc. Modern Lisp dialects such as Common Lisp and Scheme provide such syntax via datum labels, with which objects can be marked, which can then recur elsewhere, indicating shared rather than duplicated structure, enabling the reader or printer to detect and thus trigger evaluation or display of cycles without infinitely recursing

#n=(x y . #n#) The definition of an atom varies per context; in the original definition by John McCarthy, it was assumed that there existed "an infinite set of distinguishable atomic symbols" represented as "strings of capital Latin letters and digits with single embedded blanks" (a subset of character string and numeric literals). Most modern sexpr notations allow more general quoted strings (for example including punctuation or full Unicode), and use an abbreviated notation to represent lists with more than 2 members, so that

(x y z) stands for

(x . (y . (z . NIL))) NIL is the special end-of-list object (alternatively written (), which is the only representation in Scheme). In the Lisp family of programming languages, S-expressions are used to represent both source code and data. Other uses of S-expressions are in Lisp-derived languages such as DSSSL, and as mark-up in communication protocols like IMAP and John McCarthy's CBCL. It is also used as text representation of WebAssembly. The details of the syntax and supported data types vary in the different languages, but the most common feature among these languages is the use of S-expressions and prefix notation.

Datatypes and syntax There are many variants of the S-expression format, supporting a variety of different syntaxes for different datatypes. The most widely supported are:

Lists and pairs: (1 () (2 . 3) (4)) Symbols: with-hyphen ?@!$ |a symbol with spaces| Strings: "Hello, world!" Integers: -9876543210 Floating-point numbers: -0.0 6.28318 6.022e23 The character # is often used to prefix extensions to the syntax, e.g. #x10 for hexadecimal integers, or #\C for characters.

Uses

Use in Lisp When representing source code in Lisp, the first element of an S-expression is commonly an operator or function name and any remaining elements are treated as arguments. This is called "prefix notation" or "Polish notation". As an example, the Boolean expression written 4 == (2 + 2) in C, is represented as (= 4 (+ 2 2)) in Lisp's s-expr-based prefix notation. As noted above, the precise definition of "atom" varies across LISP-like languages. A quoted string can typically contain anything but a quote, while an unquoted identifier atom can typically contain anything but quotes, whitespace characters, parentheses, brackets, braces, backslashes, and semicolons. In either case, a prohibited character can typically be included by escaping it with a preceding backslash. Unicode support varies. The recursive case of the definition of S-expressions is traditionally implemented using cons cells. S-expressions were originally intended only for data to be manipulated by M-expressions, but the first implementation of Lisp was an interpreter of S-expression encodings of M-expressions, and Lisp programmers soon became accustomed to using S-expressions for both code and data. This means that Lisp is homoiconic; that is, the primary representation of programs is also a data structure in a primitive type of the language itself. Nested lists can be written as S-expressions: ((milk juice) (honey marmalade)) is a two-element S-expression whose elements are also two-element S-expressions. The whitespace-separated notation used in Lisp (and this article) is typical. Line breaks (newline characters) usually qualify as separators. This is a simple context-free grammar for a tiny subset of English written as an S-expression, where S = sentence, NP = Noun Phrase, VP = Verb Phrase, V = Verb:

Program code can be written in S-expressions, usually using prefix notation. Example in Common Lisp:

S-expressions can be read in Lisp using the function READ. READ reads the textual representation of an S-expression and returns Lisp data. The function PRINT can be used to output an S-expression. The output then can be read with the function READ, when all printed data objects have a readable representation. Lisp has readable representations for numbers, strings, symbols, lists and many other data types. Program code can be formatted as pretty printed S-expressions using the function PPRINT (note: with two Ps, short for pretty-print). Lisp programs are valid S-expressions, but not all S-expressions are valid Lisp programs. (1.0 + 3.1) is a valid S-expression, but not a valid Lisp program, since Lisp uses prefix notation and a floating point number (here 1.0) is not valid as an operation (the first element of the expression). An S-expression preceded by a single quotation mark, as in 'x, is syntactic sugar for a quoted S-expression, in this case (quote x).

Other uses S-expressions are also used for WebAssembly text format.

… excerpt ends here. Continue reading the full article.

Illustrations

S-expression: Tree data structure representing the S-expression (* 2 (+ 3 4))
Tree data structure representing the S-expression (* 2 (+ 3 4))

Worked examples

Example 1 — a first encounter with S-expression

Start with the simplest possible case. Write down what S-expression claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to S-expression before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about S-expression ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of S-expression

In research
S-expression appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses S-expression in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
S-expression is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data serialization formats, Lisp (programming language), so understanding it makes those chapters shorter.
In everyday life
Look for S-expression outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “S-expression” →

How to study S-expression in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what S-expression means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain S-expression out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is S-expression in simple terms?

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 we…

Why does S-expression matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study S-expression?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on S-expression.

Tags

  • Data serialization formats
  • Lisp (programming language)

Keep exploring