WIPIVERSE All articles

S-expression

S-expression (also written s-expression, sexpr, or sexp) is a short form of symbolic expression. In computer programming, it denotes a notation for nested list, or tree-structured, data. S-expressions were devised for and popularized by the programming language Lisp, which employs them to represent both source code and data.

Classical definition

In the conventional parenthesized syntax of Lisp, an S-expression is defined recursively as either:

  1. an atom of the form x, or
  2. an expression of the form (x . y), where x and y are themselves S-expressions.

This definition reflects Lisp's representation of lists as chains of "cells," each an ordered pair; in ordinary lists the second element points to the next cell, forming a list. Because the definition is recursive, both the underlying representation and the notation can express any binary tree. In the original formulation by John McCarthy, an infinite set of distinguishable atomic symbols was assumed, represented as strings of capital Latin letters and digits with single embedded blanks.

Surface syntax

An abbreviated notation is normally used for lists of more than two members, so that (x y z) stands for (x . (y . (z . NIL))), where NIL is the special end-of-list object (written () in Scheme). Commonly supported data types include lists and pairs, symbols, strings, integers, and floating-point numbers; the character # is frequently used to prefix syntax extensions, such as #x10 for hexadecimal integers.

When S-expressions represent source code, the first element is typically an operator or function name and the remaining elements are treated as arguments, yielding "prefix notation" or Polish notation. For example, 4 == (2 + 2) in C is written (= 4 (+ 2 2)) in Lisp. Because programs share their primary representation with data structures of the language, Lisp is described as homoiconic.

Origins and design intent

S-expressions were created by John McCarthy in the late 1950s in connection with Lisp, and were documented in his 1960 paper "Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I." They were originally intended only as a data format to be manipulated by so-called M-expressions; however, the first Lisp implementation was an interpreter of S-expression encodings of M-expressions, and programmers soon adopted S-expressions for both code and data.

Applications

Beyond Lisp and its dialects, S-expressions are used in Lisp-derived languages such as DSSSL, as markup in communication protocols including IMAP and McCarthy's CBCL, and as the text representation of WebAssembly.

In May 1997, Ron Rivest submitted an Internet Draft defining an S-expression syntax for general-purpose data storage and exchange, similar in purpose to XML. It was never approved as a Request for Comments but has been cited and used by other RFCs, notably RFC 2693, and was originally intended for the SPKI project. Rivest's format defines an S-expression as either an octet-string or a finite list of other S-expressions, and specifies three interchange formats, including a canonical representation for digital-signature purposes. Adoption outside SPKI has been limited, though implementations exist in GnuPG, libgcrypt, Nettle, and GNU lsh.

Relation to XML

S-expressions are often compared with XML. A commonly noted difference is that S-expressions have a single form of containment, the dotted pair, whereas XML tags may contain simple attributes, other tags, or CDATA, each with distinct syntax. S-expressions also do not inherently define a reference mechanism, whereas XML provides unique identifiers and references to them. For simple use cases S-expressions are simpler; for more advanced use, XML offers a query language, XPath.

Standardization

Specifications for the S-expression syntax of some Lisp-derived languages are included in their standards: Common Lisp (ANSI INCITS 226-1994 (R2004)), Scheme (R5RS and R6RS), and ISLISP.

Browse

More topics to explore

    Browse all articles