Reputation: 22510
Should numbers in scheme be quoted?
In the following examples (tested in ikarus), it seems that quoting numbers does not matter while too much quoting creates problems.
> (+ '1 1)
2
> (+ '1 '1)
2
> (+ '1 ''1)
1
What is the standard way to use numbers (e.g. in the definition of a function body)? quoted or not quoted?
Upvotes: 0
Views: 112
Reputation: 15793
R6RS's definition of quotation says so:
(quote <datum>)
syntaxSyntax:
<Datum>
should be a syntactic datum.Semantics:
(quote <datum>)
evaluates to the datum value represented by<datum>
(see section 4.3). This notation is used to include constants.
So it is correct to do '"aa"
or '123
but I have never seen it, I would find it funny to read code quoting the numbers or other constants.
In older lisps, such as emacs lisp, it is the same (in emacs lisp the syntax is called sexp
or S-Expression
instead of datun
). But the real origin of the quotation
's meaning comes from McCarthy and described in A Micro-Manual for Lisp
.
Upvotes: 0
Reputation: 48745
Numbers in Scheme are self evaluating. That means they act in the same way if they are quoted or not.
If you enter (some 1)
in DrRacket and start the Macro stepper and disable macro hiding the call will end up looking like:
(#%app call-with-values (lambda () (#%app some (quote 1))) print-values))
Thus Racket actually quotes the values that are self evaluating because their runtime doesn't support self evaluation in the core language / fully expanded program.
It might be that in some implementations a unquoted and a quoted number will be evaluated differently even if Racket threats them the same, however it would be surprising if it had any real impact.
Most programmers are lazy and would refrain from quoting self evaluating code. The exception would be as communication to the reader. Eg. in Common Lisp nil
()
and the quoted variants are all the same and could indeed used ()
everywhere, but many choose to use nil
when the object is used as a boolean and '()
if it is used as a literal list.
Upvotes: 2