tags:

views:

520

answers:

5

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 ' for, for example

(atom? 'turkey)

to avoid an "undefined variable" error. The ', according to the book, is a Common Lisp thing. Hmm. Anyway I have two questions:

  1. is the interpreter I mentioned above a good one? Can you recommend another? I need one that will go well with The Little Schemer.
  2. what is '?

Thanks.

+6  A: 

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 and replace it with its value; I really mean the name foo".

I think SISC is perfectly fine for exploring the exercises in TLS.

Jonathan Feinberg
+2  A: 

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:

(let ((x 5))
  (display x) (newline)
  (display 'x) (newline))

This displays:

5
x

In the first case, x is evaluated and passed to display, which prints 5. In the second case, the symbol x (which isn't the same thing as a character string) is passed to display, which prints the name of the symbol.

Greg Hewgill
+2  A: 

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 this.

Rainer Joswig
+1  A: 
  1. 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.

  2. 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.

Vijay Mathew
+1  A: 

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.

Quoting something does just this and the single-quote is just requires less typing and leads to less verbose code.

Brad Lucas