The Grinnell Scheme Web: Comments

Is there some way to include explanations and commentary in a Scheme program, along with the definitions and commands?

Yes. Scheme completely ignores any line that begins with a semicolon, so you can include as many comments as you like, provided that you begin each line with a semicolon. (One reason that I recommend the Emacs text editor is that it inserts such semicolons automatically when it can recognize from the file name that you're editing a Scheme program.)

If Scheme encounters a semicolon in the middle of a line, it again ignores everything from that semicolon to the end of the line, so you can attach a comment to the right of some definition or command that would otherwise be obscure.

Here is the ``Hello, world!'' program again, this time with comments included:

;;; This program prints out a cheerful greeting to the world at large.
;;; Programmer: John Stone, Department of Mathematics and Computer
;;;     Science, Grinnell College
;;; Created: June 20, 1995
;;; Last revised: June 20, 1995

(display "Hello, world!")     ; Issue the greeting.
(newline)                     ; Terminate the output line.

Why the triple semicolons? I thought you only needed one.

That's right, but many Scheme programmers follow the convention of using double, triple, or even quadruple semicolons to set off headers of different degrees of importance. In a long program, each definition might be preceded by a two-semicolon header, each block of related definitions by a three-semicolon header, and each major division of the program by a four-semicolon header. ``Wing comments'' -- those placed to the right of definitions or commands -- almost always begin with single semicolons. The number of semicolons has no effect whatever on the execution of the program. Comments are solely for the benefit of human readers.

What if a semicolon appears inside a string of characters to be printed out? Suppose we wanted to write ``Hello; goodbye'' -- would the semicolon begin a comment?

No. Between double-quotation marks, and in other contexts in which the semicolon designates a character, it is not interpreted as the beginning of a comment. The program

(display "Hello; goodbye")
(newline)
will write out both salutations.


Next topic
Previous topic
Table of contents


This document is available on the World Wide Web as

http://www.math.grin.edu/~stone/scheme-web/comments.html


created June 20, 1995
last revised December 26, 1995

Copyright 1995 by John David Stone (stone@math.grin.edu)