This question arose when reading SICP. Why (list 'quote '(a b c))
evaluated by the interpreter (R5RS in Dr.Racket) as '(a b c)
. For me it should be (quote (a b c))
. For instance (list 'quot '(a b c))
is evaluated as (quot (a b c))
. What is so special in the 'quote
?
相关问题
- Generating powerset in one function, no explicit r
- What is fixed point?
- Forming Lisp code to task — related to flatten lis
- unfold function in scheme
- Confused by Lisp Quoting
相关文章
- Does learning one Lisp help in learning the other?
- What is the definition of “natural recursion”?
- How do I write a macro-defining macro in common li
- How can I unintern a qualified method?
- Changing the nth element of a list
- sleep in emacs lisp
- How do I define a sub environment in scheme?
- Why is it legal in a function definition to make s
display
emits some behaviors. Eg.'(a . (b . (c . ())))
is displayed(a b c)
.'quote
is displayedquote
and perhaps'(quote x y)
is displayed(quote x y)
while'(quote x)
is displayed'x
or(quote x)
. Which one is implementation dependent but both mean the same.As data (ie. quoted, like
(quote quote)
and it's abbrivation'quote
) the result of the evaluation, the symbolquote
is nothing special, for any LISP, just like'+
and'potato
happen to be the symbols+
andpotato
. Any symbol that mean something when not quoted is no special when quoted.'(a b c)
and(quote (a b c))
are actually different notations for the same. So don't be surprised if your Lisp prints the shorter version.In general
'<something>
is the same as(quote <something>)
.QUOTE
is used in Lisp to mark expressions which should evaluate to themselves. Usually a list would be a function or macro call and a symbol would be a variable. If you want to treat those as data, you need to quote them.Since
(quote <something>)
is used so often in Lisp, the abbreviated version'<something>
has been introduced to save a bit of typing or reading...You'll get different behaviors depending on exactly what Lisp you're using (Scheme, Racket, Common Lisp, etc.) but in general, the system will accept
'x
as a shorthand or syntactic sugar for(quote x)
. The two forms are exactly equivalent and their values are the same: the unevaluatedx
. When a result is coming out of the system, it might choose to print in the first way to make the result more intuitive to the user. A similar thing happens withcons
, too. For instance,because that's the general way that cons cells (pairs) are printed. However, there's a special case defined for when the second part of the pair is another list (either the empty list
()
or another pair, and that's why we have the following. I've also written a bit more about how lists and cons cells are printed in an answer to Recursive range in Lisp adds a period?.Now, I've written the values of the expression above. E.g., the value of the form
(cons 1 '(2 3))
is the list(1 2 3)
. As an additional complication, some systems (I'm thinking of some languages is Dr. Racket, in particular) don't print the value of a form in the interactive prompt, but rather print a form that would produce the same (for certain interpretations of “the same”) values. For instance, you might evaluate'(1 . 2)
and see the output(cons 1 2)
because that's another form that would produce the same value. This can be helpful if you're doing pure functional programming that has referential transparency, but if you're not expecting it, it can lead to some confusion.A good way to see that we're getting the results that we should, regardless of how the system prints them, is to inspect them. We expect that
(list 'quote '(a b c))
should return a list whosecar
is the symbolquote
and whosecadr
is the list(a b c)
. This is what we get (in Dr. Racket 5.3 with language R5RS):We get similar results if we use
'qmmmt
instead of'quote
:The only difference is that in the first case,
display
displays the list whosecar
is the symbolquote
using the shorthand that is available for such lists. That is, instead of displaying(quote (a b c))
it displayed'(a b c)
.It also took me a while to understand this problem. But it's just your good-hearted lisp interpreter showing (quote (a b c)) in it's equivalent form '(a b c). Since there is no such equivalence/syntactic sugar for (quott (a b c)), it's shown as is.