Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
S-expression
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==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 logic|Boolean]] expression written {{nowrap begin}}<code>4 == (2 + 2)</code>{{nowrap end}} in [[C (programming language)|C]], is represented as {{nowrap begin}}<code>(= 4 (+ 2 2))</code>{{nowrap end}} 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 s-expr definition is traditionally implemented using [[cons cell]]s. 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. 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: <code>((milk juice) (honey marmalade))</code> 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 (Gazdar/Melish, Natural Language Processing in Lisp), where S=sentence, NP=Noun Phrase, VP=Verb Phrase, V=Verb: <syntaxhighlight lang="lisp"> (((S) (NP VP)) ((VP) (V)) ((VP) (V NP)) ((V) died) ((V) employed) ((NP) nurses) ((NP) patients) ((NP) Medicenter) ((NP) "Dr Chan")) </syntaxhighlight> Program code can be written in S-expressions, usually using prefix notation. Example in [[Common Lisp]]: <syntaxhighlight lang="lisp"> (defun factorial (x) (if (zerop x) 1 (* x (factorial (- x 1))))) </syntaxhighlight> 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. <code>(1.0 + 3.1)</code> 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 <code>'x</code>, is [[syntactic sugar]] for a [[Lisp (programming language)#Self-evaluating forms and quoting|quoted S-expression]], in this case <code>(quote x)</code>.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)