Reputation: 5878
I am having a hard time understanding symbols in Scheme. The following confuses me:
1 ]=> (symbol? 'x)
; Value: #t
1 ]=> (symbol? '('x))
; Value: #f
I thought I understood why the first one is a symbol, but then why is '('x))
not? Can someone please explain why?
For what it's worth, I am running MIT/GNU Scheme.
Upvotes: 1
Views: 826
Reputation: 31147
In scheme '...
is a shorthand for (quote ...)
.
Thus 'x
is shorthand for (quote x)
.
And '(1 2 3)
is shorthand (quote (1 2 3))
.
When a quote expression is evaluated, the quoted values is not evaluated as an expression, but simply returned.
In (quote x)
what is quoted is the symbol x
. So (quote x)
evaluates to the symbol x
.
In (quote (1 2 3))
the quoted value is a list. It evaluates to (1 2 3)
.
In your slightly more complicated example, you have
'('x)
which is shorthand for (quote ((quote x)))
.
This evaluates to the list ((quote x))
. Which in most Schemes are
printed as ('x)
.
Upvotes: 4
Reputation: 5053
'('x)
is a list, not a symbol. Symbols in Scheme are alphanumeric, like variables and keywords. So 'a
is a symbol, and so is 'supercalafragalistic
, but '(1 2 3)
is a list of numbers.
I'm not sure exactly what's throwing you off, but it's probably the '
. '
can be used to make symbols, but also to make lists, and other things too. Not everything that starts with '
is a symbol.
Upvotes: 1