<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki1463/index.php?action=history&amp;feed=atom&amp;title=S-expression</id>
	<title>S-expression - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki1463/index.php?action=history&amp;feed=atom&amp;title=S-expression"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki1463/index.php?title=S-expression&amp;action=history"/>
	<updated>2026-12-04T06:14:00Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.46.3</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki1463/index.php?title=S-expression&amp;diff=35971&amp;oldid=prev</id>
		<title>imported&gt;SchlurcherBot: Bot: http → https</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki1463/index.php?title=S-expression&amp;diff=35971&amp;oldid=prev"/>
		<updated>2026-10-04T02:27:28Z</updated>

		<summary type="html">&lt;p&gt;Bot: http → https&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Data serialization format}}&lt;br /&gt;
[[File:Corrected_S-expression_tree_2.svg|thumb|[[Tree (data structure)|Tree]] data structure representing the S-expression &amp;lt;code&amp;gt;(* 2 (+ 3 4))&amp;lt;/code&amp;gt; ]]&lt;br /&gt;
&lt;br /&gt;
In [[computer programming]], an &amp;#039;&amp;#039;&amp;#039;S-expression&amp;#039;&amp;#039;&amp;#039; (or &amp;#039;&amp;#039;&amp;#039;symbolic expression&amp;#039;&amp;#039;&amp;#039;, abbreviated as &amp;#039;&amp;#039;&amp;#039;sexpr&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;sexp&amp;#039;&amp;#039;&amp;#039;) is an expression in a like-named notation for nested [[List (computing)|list]] ([[Tree (data structure)|tree]]-structured) data. S-expressions were invented for, and popularized by, the programming language [[Lisp (programming language)|Lisp]], which uses them for [[source code]] as well as data.&lt;br /&gt;
&lt;br /&gt;
== Characteristics ==&lt;br /&gt;
&lt;br /&gt;
In the usual parenthesized [[Syntax (programming languages)|syntax]] of Lisp, an S-expression is classically defined&amp;lt;ref name=&amp;quot;McCarthy1960&amp;quot;&amp;gt;John McCarthy (1960/2006). [http://www-formal.stanford.edu/jmc/recursive/recursive.html Recursive functions of symbolic expressions] {{webarchive|url=https://web.archive.org/web/20040202215021/http://www-formal.stanford.edu/jmc/recursive/recursive.html |date=2004-02-02 }}. Originally published in [[Communications of the ACM]].&amp;lt;/ref&amp;gt; as&lt;br /&gt;
&lt;br /&gt;
# an atom of the form {{math|&amp;#039;&amp;#039;x&amp;#039;&amp;#039;}}, or&lt;br /&gt;
# an [[Expression (computer science)|expression]] of the form {{math|(&amp;#039;&amp;#039;x&amp;#039;&amp;#039; . &amp;#039;&amp;#039;y&amp;#039;&amp;#039;)}} where {{math|&amp;#039;&amp;#039;x&amp;#039;&amp;#039;}} and {{math|&amp;#039;&amp;#039;y&amp;#039;&amp;#039;}} are S-expressions.&lt;br /&gt;
&lt;br /&gt;
This definition reflects LISP&amp;#039;s representation of a list as a series of &amp;quot;cells&amp;quot;, each one an [[ordered pair]]. In plain lists, {{math|&amp;#039;&amp;#039;y&amp;#039;&amp;#039;}} points to the next cell (if any), thus forming a [[Linked list|list]]. The [[Recursion|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 reference]]s, in which case the structure is not a tree at all, but a [[cyclic graph]], and cannot be represented in classical S-expression notation&lt;br /&gt;
unless a convention for cross-reference is provided, analogous to SQL [[foreign key]]s, [[SGML]]/[[XML]] IDREFs, etc. Modern Lisp dialects such as [[Common Lisp]]&amp;lt;ref&amp;gt;{{cite web|title=Common Lisp HyperSpec: 22.4 - The Printer Dictionary: *PRINT-CIRCLE*|url=http://clhs.lisp.se/Body/v_pr_cir.htm|date=2018-12-28}}&amp;lt;/ref&amp;gt; and [[Scheme (programming language)|Scheme]]&amp;lt;ref&amp;gt;{{cite web|title=Revised&amp;lt;sup&amp;gt;7&amp;lt;/sup&amp;gt; Report on the Algorithmic Language‌ Scheme: Section 2.4: Datum Labels|url=https://small.r7rs.org/attachment/r7rs.pdf#section.2.4|date=2013-07-06}}&amp;lt;/ref&amp;gt; provide such syntax via &amp;#039;&amp;#039;datum labels&amp;#039;&amp;#039;, with which objects can be marked, which can then recur elsewhere, indicating shared rather than duplicated structure, enabling the [[Lisp reader|reader]] or [[Format (Common Lisp)|printer]] to detect and thus trigger evaluation or display of cycles without infinitely recursing&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;code&amp;gt;#n=(&amp;#039;&amp;#039;x&amp;#039;&amp;#039; &amp;#039;&amp;#039;y&amp;#039;&amp;#039; . #n#)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The definition of an atom varies per context; in the original definition by [[John McCarthy (computer scientist)|John McCarthy]],&amp;lt;ref name=&amp;quot;McCarthy1960&amp;quot;/&amp;gt; it was assumed that there existed &amp;quot;an infinite set of distinguishable [[Symbol (programming)|atomic symbols]]&amp;quot; represented as &amp;quot;strings of capital [[Latin script|Latin letter]]s and digits with single embedded blanks&amp;quot; (a subset of [[String (computing)|character string]] and numeric [[Literal (computer programming)|literal]]s).&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;code&amp;gt;(&amp;#039;&amp;#039;x&amp;#039;&amp;#039; &amp;#039;&amp;#039;y&amp;#039;&amp;#039; &amp;#039;&amp;#039;z&amp;#039;&amp;#039;)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
stands for&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;code&amp;gt;(&amp;#039;&amp;#039;x&amp;#039;&amp;#039; . (&amp;#039;&amp;#039;y&amp;#039;&amp;#039; . (&amp;#039;&amp;#039;z&amp;#039;&amp;#039; . NIL)))&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;NIL&amp;lt;/code&amp;gt; is the special end-of-list [[Object (computer science)|object]] (alternatively written &amp;lt;code&amp;gt;()&amp;lt;/code&amp;gt;, which is the only representation in [[Scheme (programming language)|Scheme]]&amp;lt;ref&amp;gt;{{Cite web|url=https://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html|title=Revised^5 Report on the Algorithmic Language Scheme|website=schemers.org}}&amp;lt;/ref&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
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 [[Document Style Semantics and Specification Language|DSSSL]], and as [[Markup language|mark-up]] in [[communication protocol]]s like [[Internet Message Access Protocol|IMAP]] and [[John McCarthy (computer scientist)|John McCarthy]]&amp;#039;s [[Common Business Communication Language|CBCL]]. It is also used as text representation of [[WebAssembly]]. The details of the syntax and supported [[data type]]s vary in the different languages, but the most common feature among these languages is the use of S-expressions and prefix notation.&lt;br /&gt;
&lt;br /&gt;
==Datatypes and syntax==&lt;br /&gt;
There are many variants of the S-expression format, supporting a variety of different syntaxes for different datatypes. The most widely supported are:&lt;br /&gt;
* &amp;#039;&amp;#039;Lists and pairs&amp;#039;&amp;#039;: &amp;lt;code&amp;gt;(1 () (2 . 3) (4))&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;Symbols&amp;#039;&amp;#039;: &amp;lt;code&amp;gt;with-hyphen&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;?@!$&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;|a symbol with spaces|&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;Strings&amp;#039;&amp;#039;: &amp;lt;code&amp;gt;&amp;quot;Hello, world!&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;Integers&amp;#039;&amp;#039;: &amp;lt;code&amp;gt;-9876543210&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;Floating-point numbers&amp;#039;&amp;#039;: &amp;lt;code&amp;gt;-0.0&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;6.28318&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;6.022e23&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The character &amp;lt;code&amp;gt;#&amp;lt;/code&amp;gt; is often used to prefix extensions to the syntax, e.g. &amp;lt;code&amp;gt;#x10&amp;lt;/code&amp;gt; for hexadecimal integers, or &amp;lt;code&amp;gt;#\C&amp;lt;/code&amp;gt; for characters.&lt;br /&gt;
&lt;br /&gt;
==Uses==&lt;br /&gt;
&lt;br /&gt;
===Use in Lisp===&lt;br /&gt;
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 &amp;quot;prefix notation&amp;quot; or &amp;quot;[[Polish notation]]&amp;quot;. As an example, the [[Boolean logic|Boolean]] expression written {{nowrap begin}}&amp;lt;code&amp;gt;4 == (2 + 2)&amp;lt;/code&amp;gt;{{nowrap end}} in [[C (programming language)|C]], is represented as {{nowrap begin}}&amp;lt;code&amp;gt;(= 4 (+ 2 2))&amp;lt;/code&amp;gt;{{nowrap end}} in Lisp&amp;#039;s s-expr-based prefix notation.&lt;br /&gt;
&lt;br /&gt;
As noted above, the precise definition of &amp;quot;atom&amp;quot; varies across LISP-like languages. A quoted string can typically contain anything but a quote, while&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
The recursive case of the definition of S-expressions is traditionally implemented using [[cons cell]]s.&lt;br /&gt;
&lt;br /&gt;
S-expressions were originally intended only for data to be manipulated by [[M-expression]]s, 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.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Nested lists can be written as S-expressions: &amp;lt;code&amp;gt;((milk juice) (honey marmalade))&amp;lt;/code&amp;gt; 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,&amp;lt;ref&amp;gt;G. Gazdar, Ch. Melish, Natural Language Processing in LISP&amp;lt;/ref&amp;gt; where S = sentence, NP = Noun Phrase, VP = Verb Phrase, V = Verb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(((S) (NP VP))&lt;br /&gt;
 ((VP) (V))&lt;br /&gt;
 ((VP) (V NP))&lt;br /&gt;
 ((V) died)&lt;br /&gt;
 ((V) employed)&lt;br /&gt;
 ((NP) nurses)&lt;br /&gt;
 ((NP) patients)&lt;br /&gt;
 ((NP) Medicenter)&lt;br /&gt;
 ((NP) &amp;quot;Dr Chan&amp;quot;))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Program code can be written in S-expressions, usually using prefix notation. Example in [[Common Lisp]]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(defun factorial (x)&lt;br /&gt;
   (if (zerop x)&lt;br /&gt;
       1&lt;br /&gt;
       (* x (factorial (- x 1)))))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;#039;&amp;#039;pretty&amp;#039;&amp;#039;-print).&lt;br /&gt;
&lt;br /&gt;
Lisp programs are valid S-expressions, but not all S-expressions are valid Lisp programs. &amp;lt;code&amp;gt;(1.0 + 3.1)&amp;lt;/code&amp;gt; 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).&lt;br /&gt;
&lt;br /&gt;
An S-expression preceded by a single quotation mark, as in &amp;lt;code&amp;gt;&amp;#039;x&amp;lt;/code&amp;gt;, is [[syntactic sugar]] for a [[Lisp (programming language)#Self-evaluating forms and quoting|quoted S-expression]], in this case &amp;lt;code&amp;gt;(quote x)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Other uses===&lt;br /&gt;
&lt;br /&gt;
S-expressions are also used for [[WebAssembly text format]].&lt;br /&gt;
&lt;br /&gt;
== Relation to XML ==&lt;br /&gt;
S-expressions are often compared to [[XML]]: one key difference is that S-expressions have just one form of containment, the dotted pair, while XML tags can contain simple attributes, other tags, or [[CDATA]], each using different syntax. Another is that S-expressions do not define a reference mechanism, whereas XML provides a notion of unique identifiers and references to them. For simple use cases, S-expressions are simpler than XML, but for more advanced use cases, XML has a query language called [[XPath]] which many tools and third party libraries use to simplify the handling of XML data.&lt;br /&gt;
&lt;br /&gt;
== Standardization ==&lt;br /&gt;
{{See also|Canonical S-expressions}}&lt;br /&gt;
&lt;br /&gt;
Standards for some Lisp-derived programming languages include a specification for their S-expression syntax.  These include [[Common Lisp]] (ANSI standard document ANSI INCITS 226-1994 (R2004)), [[Scheme (programming language)|Scheme]] (R5RS and [[R6RS]]&amp;lt;ref&amp;gt;{{Cite journal|title=Revised6 Report on the Algorithmic Language Scheme|date=Aug 12, 2009|journal=Journal of Functional Programming|volume=19|issue=S1|pages=1–301|doi=10.1017/S0956796809990074|last1=Sperber|first1=Michael|last2=Dybvig|first2=R. Kent|last3=Flatt|first3=Matthew|last4=Van Straaten|first4=Anton|last5=Findler|first5=Robby|last6=Matthews|first6=Jacob|s2cid=267822156 }}&amp;lt;/ref&amp;gt;), and [[ISLISP]].&lt;br /&gt;
&lt;br /&gt;
In May 1997, [[Ron Rivest]] submitted an [[Internet Draft]]&amp;lt;ref&amp;gt;[https://web.archive.org/web/20230223024606/http://people.csail.mit.edu/rivest/Sexp.txt  S-expressions], Network Working Group, Internet Draft, Expires November 4, 1997 - R. Rivest, May 4, 1997 draft-rivest-sexp-00.txt, Ronald L. Rivest, CSAIL MIT website&amp;lt;/ref&amp;gt; to be considered for publication as an [[Request for Comments|RFC]]. The draft defined a syntax based on Lisp S-expressions but intended for general-purpose data storage and exchange (similar to [[XML]]) rather than specifically for programming. It was never approved as an RFC, but it has since been cited and used by other RFCs (e.g. RFC 2693) and several other publications.&amp;lt;ref&amp;gt;[https://scholar.google.com/scholar?hl=en&amp;amp;lr=&amp;amp;safe=off&amp;amp;q=rivest+sexp&amp;amp;btnG=Search rivest sexp], Google Scholar (search)&amp;lt;/ref&amp;gt; It was originally intended for use in [[Simple public key infrastructure|SPKI]].&lt;br /&gt;
&lt;br /&gt;
Rivest&amp;#039;s format defines an S-expression as being either an octet-string (a series of [[byte]]s) or a finite list of other S-expressions. It describes three interchange formats for expressing this structure. One is the &amp;quot;advanced transport&amp;quot;, which is very flexible in terms of formatting, and is syntactically similar to Lisp-style expressions, but they are not identical. The advanced transport, for example, allows octet-strings to be represented verbatim (the string&amp;#039;s length followed by a colon and the entire raw string), a quoted form allowing escape characters, [[hexadecimal]], [[Base64]], or placed directly as a &amp;quot;token&amp;quot; if it meets certain conditions. (Rivest&amp;#039;s tokens differ from Lisp tokens in that the former are just for convenience and aesthetics, and treated exactly like other strings, while the latter have specific syntactical meaning.)&lt;br /&gt;
&lt;br /&gt;
Rivest&amp;#039;s draft defines a [[Canonical S-expressions|canonical representation]] &amp;quot;for digital signature purposes&amp;quot;. It is intended to be compact, easier to parse, and unique for any abstract S-expression. It only allows verbatim strings, and prohibits whitespace as formatting outside strings. Finally there is the &amp;quot;basic transport representation&amp;quot;, which is either the canonical form or the same encoded as Base64 and surrounded by [[Bracket|braces]], the latter intended to safely transport a canonically encoded S-expression in a system which might change spacing (e.g. an email system which has 80-character-wide lines and wraps anything longer than that).&lt;br /&gt;
&lt;br /&gt;
This format has not been widely adapted for use outside of SPKI (some of the users being [[GnuPG]], libgcrypt, [[Nettle (cryptographic library)|Nettle]], and [[GNU]] lsh). Rivest&amp;#039;s S-expressions web page provides [[C (programming language)|C]] source code for a parser and generator (available under the [[MIT license]]), which could be adapted and embedded into other programs.&amp;lt;ref&amp;gt;{{Cite web|url=http://people.csail.mit.edu/rivest/Sexp.txt|title=SEXP (S-expressions)|website=people.csail.mit.edu|access-date=2023-05-05|archive-date=2023-02-23|archive-url=https://web.archive.org/web/20230223024606/http://people.csail.mit.edu/rivest/Sexp.txt}}&amp;lt;/ref&amp;gt; In addition, there are no restrictions on independently implementing the format.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[CAR and CDR]]&lt;br /&gt;
* [[Fexpr]]&lt;br /&gt;
* [[Lambda calculus]]&lt;br /&gt;
* [[M-expression]]&lt;br /&gt;
* [[Comparison of data serialization formats]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* [https://github.com/mjsottile/sfsexp sfsexp] the small, fast S-expression library for C/C++ on GitHub&lt;br /&gt;
* [https://leon.bottou.org/projects/minilisp minilisp], by Léon Bottou.&lt;br /&gt;
* [http://rosettacode.org/wiki/S-expressions S-expressions on Rosettacode] has implementations of readers and writers in many languages.&lt;br /&gt;
&lt;br /&gt;
{{Lisp programming language}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Lisp (programming language)]]&lt;br /&gt;
[[Category:Data serialization formats]]&lt;/div&gt;</summary>
		<author><name>imported&gt;SchlurcherBot</name></author>
	</entry>
</feed>