These examples don't work because they're not legal Scheme. I'm not familiar with the book, but it may be the case that it has been typeset in a way which may make it slightly hard to transcribe examples from it without the mistake you've made. To see why they're not you need to think about how the system evaluates things. Here is a simple-minded and partial description of how that might work (real implementations do something much hairier than this, and this description is partial not least because it contains no explanation for how we get the bindings of things at all).
To evaluate something:
- is it something special like a string or a number or something like that, in which case its value is just itself;
- is it a symbol, in which case look up the binding for that symbol and that is the value;
- is it a compound form like
(foo ...)
? If it is, then –
- look at its first element: is it a special magic thing we know about? If it is then do an appropriate special thing (see below);
- otherwise evaluate all of the elements of the compound form in any order you like – the value of the first element should be a function, and the value is the result of applying this function to the values of the remaining elements.
So we can use these rules to try to evaluate (car ("a" "b"))
.
- the form is a compound form, so we need to take the compound form case;
car
is not any kind of special thing, so we don't hit that case;
- so, evaluating the elements of the form (I'll do it left to right for simplicity) –
car
is a symbol, so look up its value which is a function;
("a" "b")
is a compound form, and its first element is not special –
- evaluate its elements, both of which are literal strings whose value is themselves;
- and now try to apply the value of first element to the values of all the others, and fail, because it is a string not a function.
OK, so this can't work. To make it work we need to go back and look at a case in the evaluation rules: when evaluating a compound form we sneak a look a the first element to decide if it is some special magic thing we know about and in that case we do something special. There are a small number of such special magic things: the only one that we care about here is quote
. The special rule for this is:
- if we see a compound form whose first element is
quote
, which will look like (quote ...)
, then –
- it should have exactly two elements, so it should look like
(quote <x>)
for some <x>
;
- the value of the form is
<x>
with no further attempt to evaluate <x>
.
So, for instance:
- the value of
(quote x)
is the symbol x
;
- the value of
(quote (1 2 3))
is the list (1 2 3)
;
- the value of
(quote "x")
is the string "x"
(you don't need quote
in this case, since strings already evaluate to themselves by the very first rule above, but it does not hurt).
So to go back to the form we wanted to evaluate, what we actually need is (car (quote ("a" "b")))
. When evaluating this:
- the form is a compound form, so we need to take the compound form case;
car
is not any kind of special thing, so we don't hit that case;
- so, evaluating the elements of the form (I'll do it left to right for simplicity) –
car
is a symbol, so look up its value which is a function;
(quote ("a" "b"))
is a compound form –
- its first element is special, since it is
quote
;
- so use the rule for
quote
and the result is ("a" "b")
;
- now apply the value of
car
to ("a" "b")
, and the result is "a"
.
So then there is one more trick: (quote <x>)
occurs a lot, so there is a special syntax for it, which is a single '
: 'x
reads identically to (quote x)
. This just makes source code less verbose. So this gives the final form of the working version of what you're trying to do:
(car '("a" "b"))
Finally, the reason Lisps need quote
is an interesting thing to think about, but off-topic for this answer.