I had never really thought about whether a symbol could be a number in Lisp, so I played around with it today:
> '1
1
> (+ '1 '1)
2
> (+ '1 1)
2
> (define a '1)
> (+ a 1)
2
The above code is scheme, but it seems to be roughly the same in Common Lisp and Clojure as well. Is there any difference between 1 and quoted 1?
Well, they are in fact very different.
'1is however precisely the same as(quote 1).(car ''x)evaluates to the symbol ‘quote’.1is an S-expression, it’s the external representation of a datum, a number 1. To say that1is a ‘number-object’ or an S-expression to enter that object would both be acceptable. Often it is said that1is the external representation for the actual number object.(quote 1)is another S-expression, it’s an S-expression for a list whose first element is the symbol ‘quote’ and whose second element is the number 1. This is where it’s already different, syntactic keywords, unlike functions, are not considered objects in the language and they do not evaluate to them.However, both are external representations of objects (data) which evaluate to the same datum. The number whose external representation is
1, they are however most certainly not the same objects, the same, code, the same datum the same whatever, they just evaluate to the very same thing. Numbers evaluate to themselves. To say that they are the same is to say that:And
Are ‘the same’, they aren’t, they are both different programs which just happen to terminate to the same value, a lisp form is also a program, a form is a datum which is also a program, remember.
Also, I was taught a handy trick once that shows that self-evaluating data are truly not symbols when entered:
Self evaluating data truly evaluate to themselves, they are not ‘predefined symbols’ of some sorts.