The two biggest barriers to learning Scheme are probably parentheses and indenting. In Scheme, parentheses are used a little differently than in most programming languages. Indenting is also very important, because the surface syntax of the language is so regular. When reading Scheme code, experienced programmers read the indenting structure as much as the tokens. If you don't parenthesize correctly, your programs won't run correctly. And if you don't indent them correctly, they'll be hard to understand.
The syntax of Scheme is more similar to that of C or Pascal than it may appear at first glance. After all, almost all programming languages are based on nested (statements or) expressions. Like C or Pascal, Scheme is free-form, and you can indent it any way you want.
Some people write Scheme code indented like C, with closing parentheses lined up under opening parentheses to show nesting. (People who do that are usually beginners who haven't learned to use an editor properly, as we'll explain later.) They might write
;; A poorly indented if expression (if a (if b c d ) e )
rather than
;; a nicely-indented if expression (if a (if b c d) e))
The first version looks a little more like C, but it's not really easier to read. The second example shows its structure just as clearly if you know how to read Scheme, and is in fact easier to read because it's not all stretched out. The second example takes up less space on the page or a computer screen. (Space is important when you're editing code in a window and doing other things in another window--you can see more of your program at a time.)
There are a couple of things to keep in mind about parentheses in Scheme. The first thing is that parentheses are significant. In C or Pascal, you can often leave parentheses out, because of "operator precedence parsing," where the compiler figures out the grouping. More importantly, you can often add extra parentheses around expressions without affecting their meanings.
This is not true in Scheme! In Scheme, the parentheses are not just
there to clarify the association of operators. In Scheme, parentheses
are not optional, and putting extra parentheses around things changes
their meaning. For example, the expression foo
is a variable
reference, whose effect is to fetch the value of the variable foo
.
On the other hand, the expression (foo)
is a call to the procedure
named foo
with zero arguments.
(Notice that even in C, it's not generally acceptable to write a procedure
call with too few parentheses or too many: a call foo(a, b)
can't be
written just foo a, b
or as foo((a, b))
.)
In general, you have to know where parentheses are needed and where they are not, which requires understanding Scheme's rules. Some parentheses indicate procedure calls, while others are just delimiters of special forms. Luckily, the rules are simple; they should become very clear in the next chapter or two.
The other thing to know about parentheses is that they have to match. For every opening parenthesis there has to be a closing parenthesis, and of course it must be in the right place.