What is ' (apostrophe) in Lisp / Scheme?
I am on day 1 hour 1 of teaching myself Scheme. Needless to say, I don't understand anything. So I'm reading The Little Schemer and using this thing:
http://sisc-scheme.org/sisc-online.php
as an interpreter.
I need to use '
in for example
(atom? 'turkey)
to avoid an "undefined variable" error. The '
, according to the book, is a Common Lisp thing.
I have two questions:
Is the interpreter I mentioned above a good one? Can you recommend another? I need one that will go well with The Little Schemer.
What is
'
?
The form 'foo
is simply a faster way to type the special form
(quote foo)
which is to say, "do not evaluate the name foo
replacing it with its value; I really mean the name foo
itself".
I think SISC is perfectly fine for exploring the exercises in TLS.
You need to understand the basic evaluation rules of Scheme.
First:
(atom? 'turkey)
The list is a function application, so atom?
gets evaluated to a function.
'turkey
is a short hand notation for (quote turkey)
. Evaluating (quote turkey)
gives the symbol turkey
.
So next the function is applied to the symbol turkey
and a return value is computed.
Second
(atom? turkey)
Again we have a function application and atom?
gets evaluated to a function. This time turkey
is a variable. Evaluating turkey
gives the value that is bound to it - what ever it is.
So then the function is applied to the value of the variable turkey
.
Summary
turkey
is a variable, which gets evaluated to its value. 'turkey
is (quote turkey)
, which gets evaluated to the symbol turkey
.
Scheme reuses s-expressions and builds its programs out of s-expressions. This leads to the problem that sometime turkey
should be a variable and sometimes it should be the symbol. This is slightly confusing for the beginner. After some time you'll see the power behind it.