COS 371: Programming Languages
Spring 2026
Scheme Style Guide
Names
- Use descriptive variable names.
- Use
lowercasenames, possiblywith-hyphens, notwith_underscoresorcamelCase. - Use
lstas the variable name for a list. - Predicates (those that return true or false) ends in
?, likeprime?, notisPrime. - Procedures with side effect ends in
!, likeset!andset-car!. [These start half way through the semester.]
Parentheses, linebreaks, and indentation
Scheme code should be readable without counting parentheses!- Trust DrRacket's automatic indentation.
(I have never found a need to go against it.)
Select a code block and press
Tabto re-auto-indent. - Do not put parentheses on their own line.
Good:
(define factorial (lambda (n) (if (zero? n) 1 (* n (factorial (- n 1))))))Bad:(define ; bad, do NOT put a line break here; see below. factorial ( lambda ( n )(if (zero? n ) ; ^ ^ ^ ^ ^ all bad, see next item. 1 (* n (factorial (- n 1))) ) ; bad ) ; bad ) ; badNot only is the second using extra lines for no real purpose, the first style allows you to simply keep adding)until the parentheses are balanced (as judged by DrRacket's highlighting). - Do not put spaces after
(or before); do put space between) (. - Put subexpressions all on the same line or all on different lines.
Good:
(+ 1 2 3 4 5) (+ (- 1 2) (* 3 4 5) (/ 6 7))
Bad:(+ (- 1 2) (* 3 4) 5 (/ 6 7))
It's easy to miss the5hiding on the same line as(* 3 4). - Leave the first subexpression on the same line in
define,lambda,if,letforms. (See above and below.) Thecondform may be an exception to this rule: - Line up
condclauses.(define fib (lambda (n) (cond ((<= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2))))))) - Add blank lines as appropriate. Just like writing English in paragraphs. Don't add too many blank lines (no one wants to read an essay consisting of single-sentence paragraphs) nor too few (pages of text with no organization).
- Don't have excessively long lines. Typically try to keep lines shorter than 80 characters.
Scheme features
(This section may not make sense yet.)- Avoid using boolean literals
#tor#f. Likely the expression can be simplified:(if (equal? a b) #t #f) --> (equal? a b) (if a b #f) --> (and a b)
- Use
condinstead of a nest ofifforms. - Consider using
let*instead of a nest ofletforms. - Avoid global variables.
- Avoid assignment/mutation. [Even after being granted such power in the second half of the semester.]
Documentation
- Use
;;; triple semicolonsat the top of a file to document author name(s) and the purpose of the program. - Use
;; double semicolonsto document each procedure, describing briefly what it does. - Use
; single semicolonto add short comments about certain lines, but... - Do not comment every line of code.
- Comment on tricky things.
References
Here are some style guides I looked at when compiling this document.- Eli Barzilay: extremely good style guide for Racket, which is very close to Scheme.
- Scheme wiki: explains some of the rationale behind these guidelines quite well.
- Dartmouth: good examples and explanations.