Quoting numerical constants in Chez Scheme

61 views Asked by At

I am curious as to why Chez Scheme does not treat numbers as symbols. Whether they are in a list or are quoted alone, number? returns true, meaning that it was not made into a symbol. Is there a practical reason for this?

Chez Scheme Version 9.5.4
Copyright 1984-2020 Cisco Systems, Inc.

> (number? (car '(1 2 3 4 5)))
#t
> (symbol? (car '(1 2 3 4 5)))
#f
> (define symbolic-num '5)
> (number? symbolic-num)
#t
> (symbol? symbolic-num)
#f
> 


1

There are 1 answers

1
molbdnilo On BEST ANSWER

This is not specific to Chez, but is standard behaviour; see e.g. R5RS:

The rules for writing a symbol are exactly the same as the rules for writing an identifier [6.3.3 Symbols]

So a numeric literal can't be a symbol, because it's not an identifier.

Now, 'e is shorthand for (quote e), and

(quote <datum>) evaluates to <datum>. [4.1.2 Literal expressions]

That is, (quote 1) - '1 - evaluates to 1, which is an integer, and (quote a) - 'a - evaluates to a, which is a symbol.

Numerical constants, string constants, character constants, and boolean constants evaluate ``to themselves''; they need not be quoted. [4.1.2 Literal expressions]

This gets a bit confusing because REPLs print some things in the "shorthand-quoted" form, but that's just an output convention.
Note that (display 'a) will show a, not 'a.