'(a 'b) gives out the answer (a 'b). How does this work when there is no binding for a (which is unquoted).
相关问题
- Generating powerset in one function, no explicit r
- What is fixed point?
- Relation between Function1 and Reader Monad
- scala passing function with underscore produces a
- Combining n vectors into one vector of n-tuples
相关文章
- Is there something like the threading macro from C
- Learning F#: What books using other programming la
- Creating a list of functions using a loop in R
- Does learning one Lisp help in learning the other?
- What is the definition of “natural recursion”?
- When to use interfaces, and when to use higher ord
- Functors in Ocaml
- Java Lambda Referencing Enclosing Object: Replace
The quote that prefixes the list expression prevents the evaluation of the components. If you were to write, say, (list a 'b), you'd end up with a problem if there was no binding.
This is what happens when we evaluate the expression:
The
'
quote is shorthand for thequote
special form, see the linked documentation for more details:As you can see, it prevents the quoted arguments from being evaluated, so it doesn't matter if
a
is undefined, becausea
is not interpreted as a variable inside a quoted expression. It's a different thing if we tried to build a list like this:The above will produce an error, because
a
is an undefined variable, whereasb
is a quoted expression. This will work, though - because both elements in the list are quoted:And here's another way to build the list shown in the question:
'something
is syntactic sugar for(quote something)
.So
'(a 'b)
is(quote (a (quote b)))
.quote
is not a function; it is a special form that converts the literal expression to the right into a runtime structure that looks like that expression.(quote 42)
evaluates to42
(quote apple)
evaluates to the same as(string->symbol "apple")
(quote (x 5 (a b)))
evaluates as(list (quote x) (quote 5) (quote (a b)))
In
(quote (a (quote b)))
, the second "quote" is not treated specially from any other identifier, because the right side of the first quote is taken literally, and none of it is evaluated as Scheme code.So,
(quote (a (quote b)))
evaluates to the same as(list (string->symbol "a") (list (string->symbol "quote") (string->symbol "b")))
. In other words, it's a list of 2 elements, the first of which is the symbol "a", and second of which is another list of 2 elements, the first of which is the symbol "quote", and the second of which is the symbol "b".Then we have to get into how the Scheme REPL prints out values.
( )
), with recursively printed result of each of the list items inside.(quote something)
(which would happen if it is printing a list of 2 elements, the first of which is the symbol "quote"), it will instead print'something
.Therefore, the above list, which if not for the exception would be printed as
(a (quote b))
, will actually be printed as(a 'b)
.