Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between 1 and '1 in Lisp?

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?

like image 949
Jason Baker Avatar asked Jun 03 '10 14:06

Jason Baker


People also ask

What is a lisp-1?

Lisp-1 refers to Scheme's model and Lisp-2 refers to Common Lisp's model. It's basically about whether variables and functions can have the same name without clashing. Clojure is a Lisp-1 meaning that it does not allow the same name to be used for a function and a variable simultaneously.

Is clojure a lisp-1 or LISP 2?

Clojure is a lisp-1, meaning it uses the same name resolution for both functions and value bindings.

How to define function in LISP?

Use defun to define your own functions in LISP. Defun requires you to provide three things. The first is the name of the function, the second is a list of parameters for the function, and the third is the body of the function -- i.e. LISP instructions that tell the interpreter what to do when the function is called.

What does apostrophe mean in Lisp?

The single-quote character is shorthand way of saying (quote foo) where quote is the form to return just foo without evaluating it. One thing to really remember in Scheme or any Lisp for that matter is that everything is evaluated by default. So, in cases where you don't want to evaluate you need a way to sat this.


1 Answers

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.

like image 193
Xach Avatar answered Oct 10 '22 05:10

Xach