In C or Pascal, a call to procedure foo
with arguments bar
and baz
is written
foo(bar,baz);
but in Scheme it's written
(foo bar baz)
Note that the procedure name goes inside the parentheses, along
with the arguments. Get used to it. It may seem less odd if you think
of it as being like a operating system shell command--e.g., rm foo
,
or dir bar
---but delimited by parentheses.
Just as in C, expressions can be nested. Here's a call to a procedure
foo
, with nested procedure call expressions to compute the arguments.
(foo (bar x) (baz y))
This is pretty much equivalent to C's
foo(bar(x),baz(y));
As in C or Pascal, the argument expressions in a procedure call are evaluated before actually calling the procedure; the resulting values are what's passed to the procedure. In Scheme terminology, we say that the procedure is applied to the actual argument values.
You'll notice soon that Scheme has very few special characters, and
that expressions are generally delimited by parentheses or spaces.
For example, a-variable
is a single identifier, not a subtraction
expression. Identifiers in Scheme can include not only alphabetic
characters and digits, but several other characters, such as !
,
?
, -
, and _
. Long identifiers are often
constructed from phrases, to make it clear what they mean, using hyphens
to separate words; for example, you can have a variable named
list-of-
first-ten-
lists
. You can use characters
like +
, -
, *
, and /
within an identifier,
as in before-tax-total+tax
, or estimate+epsilon
.
One consequence of Scheme's liberal rules for constructing identifiers
is that spaces are important. You must put one or more spaces
(or carriage returns) between identifiers except where special characters
(usually parentheses) make the divisions obvious. For example, the
addition expression (+ 1 a)
can't be written (+1 a)
or
(+1a)
or (+ 1a)
. (It can be written ( + 1 a )
,
because extra whitespace between tokens is ignored.)