Spelling of Minimal Scheme

(Note to computer-language purists: what I'm calling "spelling" is approximately what language geeks call "lexical structure", albeit with some semantics as it seems to come up naturally.)

Outline


Numbers
integers
a sequence of as many digits (0-9) as you wish. You can also put a plus ("+") or minus ("-") sign in front, with no spaces in between. Scheme can handle numbers as long as hundreds or thousands of digits accurately.

Example: 908723609587129087602439873245

fractions
two integers (see above) separated by a slash ("/") with no spaces in between.

Examples:

  • 2390874/132948576
  • 2390874 /132948576
    Illegal because it contains a space. Scheme treats it as two separate expressions: 2390874 (a perfectly good integer) and /132948576 (a presumably-undefined variable name).
  • 2390874 / 132948576
    Scheme treats this as three separate expressions: the number 2390874, the predefined function-name /, and the number 132948576.
floating-point numbers
a sequence of digits, a period, and another sequence of digits, with no spaces in between. You can also use scientific notation by putting the letter "e" or "E" and a positive or negative integer at the end, with no spaces in between.

Examples:

  • 3.1415926
  • 6.02e23
  • -.738E-17

Notes: Such numbers are usually inexact: for example, (* 49.0 (/ 1.0 49.0)) will probably be very close, but not exactly equal, to 1.0. They are limited in precision -- only about fifteen digits after the decimal point, and the exponent can be no larger than about 308 (depending on what's in front of the decimal point)

Spaces and line breaks
A space or line break is necessary to separate one name or number (see above) from another. They are allowed, but not required, before and after parentheses, square brackets, and curly braces. (The TAB key means something special in DrScheme; see below.)

Scheme doesn't care whether you put a whole expression on one line or on several, but if you choose to break an expression over several lines, DrScheme will help you see its structure by indenting.

Examples:

Notes: If DrScheme indents things in a way you don't think is correct, it probably means you've got one too many or one too few parentheses on the previous line. If you've changed part of a line, it's often advisable to press the TAB key, which tells DrScheme to re-indent this line in accordance with the parenthesis nesting.

Parentheses, brackets, and braces
Left parentheses must match right parentheses. Left square brackets must match right square brackets. Left curly braces must match right curly braces. Simple enough?

Examples:

Note: When you type a right parenthesis, bracket, or brace, DrScheme will automatically figure out what it's supposed to match, change it to the appropriate punctuation mark, and shade everything in between. If you were expecting to match a parenthesis and DrScheme puts in a bracket, or if the shaded portion isn't what you expected, you've got a mismatch somewhere in between.

Names
Functions, operators, programs, variables, and keywords all follow basically the same spelling rules. They can be made up of upper-case and lower-case letters (yes, case matters!), hyphens, underscores, and a few other punctuation marks. They cannot contain spaces. Nor can they contain parentheses, curly braces, square brackets, apostrophes, commas, or quotation marks, as these all have special meanings in Scheme.

Examples:

Comments
Anything between a semicolon and the end of the line is ignored by Scheme (unless the semicolon is inside a string).
Examples:
Symbols
A symbol follows the same spelling rules as the name of a variable, function, or keyword, except that it must start with an apostrophe. (This is something of an oversimplification, but it's close enough to the truth to get through a first-semester course.)

Examples:

Notes:

Examples of use:

Strings
A string is any sequence of characters surrounded by "double-quote marks". Spaces, upper and lower case letters, line breaks, and any punctuation mark are allowed inside. If you want to put a double-quote mark inside a string, precede it with a backslash, e.g.
"This is a \"string\" containing quotation marks."
If you want to put a backslash inside a string, precede it with a backslash, e.g.
"This is another string containing a single \\ backslash."

Since line breaks are allowed inside a string, if you forget the closing quote Scheme will let you go on to the next line and the next, patiently waiting for you to end the string.

(cond [(> grade 90) "A"]
      [(> grade 80) "B"]
      [(> grade 70) "C]
        [(> grade 60) "D"]
        [else "F"])
         ]
         ]
         ]]]]]]]]
By the unexpected indentation, one could tell something was funny here. Because the quotation mark after C was omitted, everything from there to just before D is treated as a single string; D is treated as a variable name, followed by a string running to just before F, then the variable name F, then a string that still hasn't ended (no matter how many close-brackets you put in).

Last modified: Tue Jun 13 10:55:55 EDT 2000
Stephen Bloch / sbloch@adelphi.edu