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:
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
'
?
If you looking for a best IDE for
scheme
then go forDr Racket
. But when startDr Racket
first line should be#lang scheme
sinceDr Racket
has many language we have explicitly mention which language we are going to use.When we want to pass an argument itself instead of passing the value of the argument then we use quote. It is mostly related to the procedure passing during using lists, pairs and atoms which are not available in C programming Language ( most people start programming using C programming, Hence we get confused) This is code in Scheme programming language which is a dialect of lisp and I guess you can understand this code.
The last line (atom? 'abc) is passing abc as it is to the procedure to check if abc is an atom or not, but when you pass(atom? abc) then it checks for the value of abc and passses the value to it. Since, we haven't provided any value to it
You need to understand the basic evaluation rules of Scheme.
First:
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 symbolturkey
.So next the function is applied to the symbol
turkey
and a return value is computed.Second
Again we have a function application and
atom?
gets evaluated to a function. This timeturkey
is a variable. Evaluatingturkey
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 symbolturkey
.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.SISC is good, but an even more lightweight online Scheme executor is http://codepad.org. It's not actually a REPL in that it's not interactive, but it's pretty close. Code you submit is executed on the server side instead of using a browser applet. And you can share code that you're running by short URL.
The about page on codepad says it uses "MzScheme v372 [cgc]".
I use codepad for all kinds of quick snippet testing (including testing code samples for SO answers!).
For the quote syntax, the difference can be seen using code like this:
This displays:
In the first case,
x
is evaluated and passed todisplay
, which prints 5. In the second case, the symbolx
(which isn't the same thing as a character string) is passed todisplay
, which prints the name of the symbol.The form
'foo
is simply a faster way to type the special formwhich is to say, "do not evaluate the name foo and replace it with its value; I really mean the name foo".
I think SISC is perfectly fine for exploring the exercises in TLS.
I suggest that you move to a better environment like PLT Scheme, which has an IDE, debugger and lots of libraries. As you move forward and start writing larger programs, you will need them.
The single-quote character is syntactic sugar for the "quote" expression, so 'turkey is the same as (quote turkey). Basically, what "quote" does is to switch off the Scheme evaluator. In other words, "quote" returns the expression, verbatim. If there was no "quote", then Scheme would try to evaluate "turkey" in the current environment. This is not a Common Lisp thing but a Lisp thing. Common Lisp and Scheme are two dialects of Lisp. The uses of "quote" are explained in all Lisp tutorials/books. Also see the answers to this question.
Shorthand for
(quote ...)
,'
turns code into data.stuff
is a symbol, that means it can be a name of a variable or name of a function, etc..'stuff
gives you the symbol "stuff" itself.(dostuff "on" those 4 :parameters)
when evaluated, would run functiondostuff
with four parameters: string, content of variable those, number and keyword.'(dostuff "on" those 4 :parameters)
when evaluated would return the code above, which, when evaluated, would in turn run functiondostuff
with that four parameters..For example: Run
'''somecode
, it returns''somecode
. Run''somecode
, it returns'somecode
. Run'somecode
, it returnssomecode
. Runsomecode
, and... well...somecode
will run.You can say that
'
is a bit like the opposite of(eval..)
.(eval (eval (eval '''(print "hello"))))
would print "Hello".(eval (eval (eval ''''(print "hello")))
- notice one more'
theneval
- would not print anything, but it would return the code(print "hello")
itself!!Except that lispers tend to call that returned code (and sometimes even handwritten code) "list" instead of "code", for reasons that will be bleeding obvious as you dig just a bit deeper. Good luck :)