Difference between sexp and list in Emacs?

1.3k views Asked by At

In Emacs there are commands to move cursor across expressions delimited in parentheses (or any brackets), namely forward-sexp, backward-sexp, forward-list and backward-list. In Lisp and any other code they behave similarly, so i see no difference between *-sexp and *-list except the last do not work inside comments or quotes.

What is the functional difference between sexp and list commands, and when should i use which?

Just in case, i understand the up-list and down-list commands, they are irrelevant to the topic.

2

There are 2 answers

0
phils On BEST ANSWER

A list is one example of an s-expression, so any function which operates on s-expressions should work on lists (but not necessarily vice-versa, as there are also non-list sexps).

The elisp manual says:

A Lisp object that is intended for evaluation is called a "form" or "expression"(1). The fact that forms are data objects and not merely text is one of the fundamental differences between Lisp-like languages and typical programming languages. Any object can be evaluated, but in practice only numbers, symbols, lists and strings are evaluated very often.

---------- Footnotes ----------

(1) It is sometimes also referred to as an "S-expression" or "sexp", but we generally do not use this terminology in this manual.

C-hig (elisp) Intro Eval RET

0
Thomas On

Not all s-expressions are lists, e.g. variables are s-expressions while they're obviously not lists.

Consider the following example:

foo (bar)

If you place the point at the beginning of the line, forward-sexp will move the point to the end of "foo" while forward-list will move the point to the end of "(bar)"