Scheme is an expression-based and value-oriented programming language. To compute in Scheme means to produce values. To program in Scheme means to write-down expressions and definitions. In this section, we explain the very basic grammatical rules that explain how to write Scheme programs; that's the syntax of the language. We don't explain what these phrases mean.
The syntax of Scheme is simple. Almost everything in Scheme is an expression and an expression come in only three shapes:
or parenthesized. Every parenthesized expression starts with ``('' and ends with ``)'' and all these parentheses matter. Adding a pair of parentheses or omitting one makes a difference.
So the syntax is simple but we know that people occasionally forget even the simplest of rules. Hence, DrScheme can help those forgetful souls out there. If you aren't sure whether you are one of them, go to the language menu and choose the Beginning Student Scheme language (under HtDP) and work your way up. It's a small subset of Scheme and helps beginners recognize mistakes with parentheses.
Here are some important parenthesized expressions:
(respond-to "hello, my friend")
(cond [(> x 0) "positive"] [(= x 0) "zero"] [(< x 0) "negative"])
(set! x (+ x 1))
In addition to expressions, Scheme also has definitions. They are also parenthesized sentences, but they don't evaluate to anything. They simply add names to Scheme's basic dictionary. There are three kinds of definitions that matter to us at the moment:
a function definition:
(define (f x) (+ (* 3 x x) (* -1 x) 9))
a structure definition:
(define-struct person (name gender yob))
a variable definition:
(define x 10)
Of course, some definitions, such as function and variable definitions, contain expressions. As we study Scheme in more detail, we will see that expressions may also introduce definitions.
This is all there is to Scheme's syntax. Using these basic rules, we can write down entire ``essays'' in Scheme, i.e., programs. In Scheme, a program consists of a series of definitions followed by an expression (and sometimes several). The definitions define an extension of Scheme's basic vocabulary; the expression naturally refers to these definitions. The value of this last expression is the value of the program.
We can also omit the definitions, in which case we get simplistic programs. For example, we get the (in)famous Hello-World program:
This is a program without any definitions and just one expression. The value of
the expression is the string
"hello world". So this is all there is to
the Hello-World program.
To understand larger programs than this kind of one-liner, we need to study what kind of values we can compute with our expressions. So let's move on and start with the simple ones.