What is the difference between 1 and '1 in Lisp?
Asked Answered
D

7

33

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?

Dympha answered 3/6, 2010 at 14:15 Comment(1)
I went into quite a lot of detail answering basically the same question here: https://mcmap.net/q/452917/-using-quote-in-clojure -- maybe that answer could be helpful to you.Malo
F
9

Well, they are in fact very different. '1 is however precisely the same as (quote 1). (car ''x) evaluates to the symbol 'quote'.

1 is an S-expression, it's the external representation of a datum, a number 1. To say that 1 is a 'number-object' or an S-expression to enter that object would both be acceptable. Often it is said that 1 is 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:

(+ 1 (* 3 3))

And

(if "Strings are true" (* 5 (- 5 3)) "Strings are not true? This must be a bug!")

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:

(let ((num 4))
  (symbol? num) ; ====> evaluates to #f
  (symbol? 'num) ; ====> evaluates to #t
  (symbol? '4) ; ====> evaluates to #f
  (symbol? '#\c) ; #f again, et cetera
  (symbol? (car ''x)) ; #t
  (symbol? quote) ; error, in most implementations
)

Self evaluating data truly evaluate to themselves, they are not 'predefined symbols' of some sorts.

Fresno answered 4/6, 2010 at 17:49 Comment(0)
U
26

In Common Lisp, '1 is shorthand for (QUOTE 1). When evaluated, (QUOTE something) returns the something part, unevaluated. However, there is no difference between 1 evaluated and 1 unevaluated.

So there is a difference to the reader: '1 reads as (QUOTE 1) and 1 reads as 1. But there is no difference when evaluted.

Unicef answered 3/6, 2010 at 15:2 Comment(1)
This apply to all self evaluated expressionPermittivity
B
14

Numbers are self-evaluating objects. That's why you don't have to worry about quoting them, as you do with, say, lists.

A symbol can be made from any string. If you want the symbol whose name is the single character 1, you can say:

(intern "1")

which prints |1|, suggesting an alternate way to enter it:

'|1|
Bhili answered 3/6, 2010 at 14:39 Comment(1)
I'm using clojure 1.1.0...'|1| evaluates to |1|. |1| brings back...Unable to resolve symbol: |1|Adrianaadriane
C
11

Quoting prevents expressions from being evaluated until later. For example, the following is not a proper list:

(1 2 3)

This is because Lisp interprets 1 as a function, which it is not. So the list must be quoted:

'(1 2 3)

When you quote a very simple expression such as a number, Lisp effectively does not alter its behavior.

See Wikipedia: Lisp.

Chelsae answered 3/6, 2010 at 14:20 Comment(4)
Why did this get upvotes? This doesn't answer the question at all! The right answer was "numbers evaluate to themselves", as below.Antibaryon
This answer got upvotes because it explains why numbers evaluate to themselves and provides a link to a Wikipedia article with further information. It does in fact answer the question.Chelsae
But 7-dimensional arrays and lambda forms, for example, are self-evaluating objects while a cons cell is not, and I'm not sure I'd call the former examples "simpler" than the latter. So this only works if you define "very simple expression" as "self-evaluating object". :-)Bhili
I am a noobie to lisp-y languages and this explanation (a) is relevant to the OP and (b) helpfulRagamuffin
F
9

Well, they are in fact very different. '1 is however precisely the same as (quote 1). (car ''x) evaluates to the symbol 'quote'.

1 is an S-expression, it's the external representation of a datum, a number 1. To say that 1 is a 'number-object' or an S-expression to enter that object would both be acceptable. Often it is said that 1 is 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:

(+ 1 (* 3 3))

And

(if "Strings are true" (* 5 (- 5 3)) "Strings are not true? This must be a bug!")

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:

(let ((num 4))
  (symbol? num) ; ====> evaluates to #f
  (symbol? 'num) ; ====> evaluates to #t
  (symbol? '4) ; ====> evaluates to #f
  (symbol? '#\c) ; #f again, et cetera
  (symbol? (car ''x)) ; #t
  (symbol? quote) ; error, in most implementations
)

Self evaluating data truly evaluate to themselves, they are not 'predefined symbols' of some sorts.

Fresno answered 4/6, 2010 at 17:49 Comment(0)
T
1

In Lisp, the apostrophe prevents symbols to be evaluated. Using an apostrophe before a number is not forbidden, it is not necessary as the numbers represent themselves. However, like any other list, it automatically gets transformed to an appropriate function call. The interpreter considers these numbers coincide with their value.

Tufted answered 3/6, 2010 at 17:40 Comment(4)
Function call? Which function?Limn
I meant to write QUOTE (function call)!Tufted
QUOTE is not a function. In Lisp the apostrophe prevents the evaluation of anything. During read time it gets transformed into (quote ...). QUOTE is a special form which just returns its argument unevaluated. The Lisp evaluator (interpreter or compiler) has a special evaluation rule for that.Limn
@Rainer I agree with you. I simply stated that in Lisp, function calls are written as lists.Tufted
L
1

As has been pointed out, there is no difference, as numbers evaluate to themselves. You can confirm this by using eval:

(eval 1)  ;=> 1

This is not limited to numbers, by the way. In fact, in Common Lisp, most things evaluate to themselves. It's just that it's very rare for something other than numbers, strings, symbols, and lists to be evaluated. For instance, the following works:

(eval (make-hash-table))  ;equivalent to just (make-hash-table)
Longs answered 3/6, 2010 at 19:30 Comment(0)
E
1

In Lisp, quote prevent the following expression to be evaluated. ' is a shorthand for quote. As a result, '1 is same as (quote 1).

However, in Lisp, symbols can never be a number. I mean, 'abc is a symbol, but '123 is not (evaluated into) a symbol. I think this is wrong of the design of Lisp. Another case is not only #t or #f can be used as a Boolean expression.

Electronics answered 26/8, 2014 at 2:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.