Question

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.

Was it helpful?

Solution

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).

OTHER TIPS

'('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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top