Next: References
Up: Introductory Scheme
Previous: Development and Debugging
This section discusses some style guidelines for Scheme, constructs to avoid,
and pointers on good programming style and efficiency.
-
Variable naming. In Scheme, variables must begin
with a letter, and all subsequent characters may be letters, numbers, or any of
the following:
+ - . * / < = > ! ? : $ % _ & ~ ^
The length of variable names is unrestricted. Thus, one should choose
descriptive variable names, avoiding abbreviations whenever practical.
Customarily, predicates end with a `?', with a few
exceptions, such as the numerical predicates. -
Assignment forms. Scheme does provide an assignment operator,
set!. In addition, Scheme provides destructive versions of many functions;
for instance, append!, given two lists, destructively appends the second
list to the end of the first. These side-effecting functions are called
assignment forms, and their names usually end with `!'. While they are
often used in real applications, from the point of view of functional
programming, they should be considered forbidden.
-
cadadr and friends. Scheme provides compositions of car and
cdr formed by inserting appropriate a's and d's between the
c and the r. For instance, (cadr l1) denotes (car (cdr
l1)). These compositions should be avoided, especially the longer ones. It
is doubtful that anyone, even accomplished Scheme programmers, has an intuitive
feel for what cadadr means, and using such forms generally means you are
doing something in an ugly way.
-
Pretty-printing. Most Scheme programmers lay out their functions as I
have throughout this manual, indenting the appropriate function calls and
adding all the closing parentheses at the end, like so:
(define (append l1 l2)
(cond
((null? l1) l2)
(else (cons (car l1) (append (cdr l1) l2)))))
To simplify matching parentheses, however, you may find it convenient to line
up the closing parentheses with the expression they are closing, as with the
brackets in C, like so:
(define (append l1 l2)
(cond
((null? l1) l2)
(else (cons (car l1) (append (cdr l1) l2)))
)
)
This is purely a matter of personal preference. -
Applying car and cdr to the empty list . In many
implementations of Scheme, including XScheme, applying car or cdr
to the empty list is valid, and returns nil in both cases. However, this
behavior is not specified in the Scheme standards, and in other implementations
it may cause an error or a warning. Thus, for reasons of portability and
general good programming style, one should avoid situations in which (car
'()) or (cdr '()) get evaluated.
-
Sequential constructs. Scheme offers a construct for sequential
programming called begin . This allows sequential execution of a number
of Scheme statements, with the return value of the begin being the last
statement executed within its body. In addition, some constructs, such as
let, perform an implicit begin. This should be avoided (except for its
use in debugging, as described in Section 11), and is basically useless in
the absence of assignment forms anyway.
Next: References
Up: Introductory Scheme
Previous: Development and Debugging
Steven J. Zeil
Tue Mar 4 14:36:27 EST 1997