问题
I am reading Programming in Emacs Lisp
Here is another list, this time with a list inside of it:
'(this list has (a list inside of it))
I am confused with the nested list, why it has not a prefix quoting as
'(this list has '(a list inside of it))
if not has a prefix `, why it not parse the a as a function?
回答1:
's-expression
is an abbreviation for (quote s-expression)
: anything inside the s-expression is considered a datum and it is not evaluated.
So,
'(this list has (a list inside of it))
is an abbreviation of:
(quote (this list has (a list inside of it)))
that contains the following list:
(this list has (a list inside of it))
which is the value of the entire quote
form since it is not evaluated.
It is easy to verify this by writing:
'(this list has '(a list inside of it))
This, if evaluated, will produce as value the following list:
(this list has (quote (a list inside of it)))
回答2:
That's one of the slight difficulties in Lisp: a list is data and can also be a program. If you want a list to be data in a Lisp program, you need to quote it.
Lists as such: one can read them with READ
(this list has (a list inside of it))
(this list has no list inside of it)
(+ 1 2)
(1 2 +)
(1 + 2)
(quote (this list has (a list inside of it)))
(quote (this list has (quote (a quote list inside of it))))
(quote quote)
Valid Lisp forms: one can evaluate them with EVAL
(+ 1 2)
Evaluates to -> 3
(quote (+ 1 2))
Evaluates to -> (+ 1 2)
(quote (this list has (a list inside of it)))
Evaluates to -> (this list has (a list inside of it))
(quote quote)
Evaluates to -> quote
This is also a valid Lisp form:
(quote (this list has (quote (a quoted list inside of it))))
It evaluates to:
(this list has (quote (a quoted list inside of it)))
来源:https://stackoverflow.com/questions/56523328/the-list-inside-a-list-of-lisp-tutorial