# 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:

• ```(define(*7 x)(* 7 x))
```
has only the spaces that are absolutely necessary. A somewhat more readable version is ...
• ```(define (*7 x) (* 7 x))
```
Even better, ...
• ```(define (*7 x)
(* 7 x))
```
illustrates both the Scheme programmers' convention of putting the function header on one line and the body on the next one or more lines, and DrScheme's automatic indentation.
• ```(
define

(     *7
x

)

(  *
7      x)

)
```
still defines exactly the same function, although it has so many spaces and line breaks that it's hard to read.

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. If the indentation is still "incorrect", don't change the indentation; fix the parentheses. Changing the indentation alone won't make the program correct; it'll only make it look correct to a human being.

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:

• `(+ 3 (* 4 5))`
is legal.
• `(+ 3 (* 4 5)`
is illegal because there are more left parentheses than right parentheses.
• ```(cond [(<= n 0) 1]
[else (* n x)])
```
is legal. Note that by convention, we use square brackets around each clause of a `cond`; parentheses or braces would also be legal.
• ```(cond ([<= n 0) 1]
[else [* n x]))
```
is illegal because the left-bracket before ```<= n 0``` doesn't match the right-parenthesis after it, and the left-parenthesis before ```[<= n 0) 1``` doesn't match the right-bracket after it, and the left-bracket before `else [* n x]` doesn't match the right-parenthesis after it.

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:

• `+`
is a good name for a function. It happens to be predefined to mean the addition function, but if you really wanted to be intentionally confusing, you could redefine it as a variable or as a completely different function.
• `sqrt`
is another good name for a function. It happens to be predefined to mean a function that computes square roots.
• `square`
is another good name; it doesn't happen to be predefined at all, but you can easily define it to mean a function that multiplies a number by itself, or you can define it to be a variable that holds a description of a geometric shape, or....
• `true`
is the predefined name of a Boolean constant. Again, if you wanted to be really confusing, you could in principle redefine it to mean something else. Please don't.
• `my age`
is an illegal name, because it contains a space. In most contexts, Scheme will try to treat this as two variable names, probably producing some kind of error message.
• `my-age`
is a perfectly good name (although it wouldn't be in an infix language like C, C++, Pascal, or Java).
• `my_age`
is another legal name, but it's very easily confused with the above. The hyphen is more commonly used than the underscore by most Scheme programmers, so you should probably avoid the underscore to avoid confusion.
• `myAge`
is another legal name, although it follows Java naming conventions and would look strange to most Scheme programmers.
• `define`
is a predefined keyword in the language. You cannot redefine it to mean something else. Other common keywords are `cond`, `else`, and `define-struct`.
• `arr[x]`
is an illegal name, because it contains square brackets.
• `*7`
is a legal name, and would actually be a very reasonable name for a function that multiplied its argument by 7.
• `+7`
is not a legal name, because it's treated as a number instead (remember, numbers can start with a plus or minus sign).
Anything between a semicolon and the end of the line is ignored by Scheme (unless the semicolon is inside a string).
Examples:
• `; This is a comment that takes up the whole line.`
• `(define bignum 1234567890);a really big number`
Note that spaces are not required around the semicolon (but they're recommended, for readability).
• `(define biggernum 1234578901234567890) ; a really really`
`                really big number`
Note that the second line is not ignored, and will presumably produce error messages when DrScheme tries to treat it as Scheme code.
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:

• `'bluebird`
• `'+`
• `'my age`
is illegal because it contains a space.

Notes:

• There is no particular connection between the symbol `'bluebird` and the variable-name ``` bluebird```, or between the symbol `'+` and the predefined function-name `+`.
• A variable may or may not "have a value". A symbol behaves more like a number: it is a value.

Examples of use:

• `'bluebird` (should return exactly the same thing)
• `(define this-bird 'bluebird)`
• `this-bird` (should return 'bluebird)
• `(symbol=? this-bird 'crow)` (should return false)
• `(symbol=? this-bird 'bluebird)` (should return true)
• `(symbol=? this-bird bluebird)` (should produce an error message because the variable "bluebird" is undefined; note that since it doesn't start with an apostrophe, Scheme treats it as a variable rather than a symbol)
• (symbol=? this-bird 'this-bird) (should return false)
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"]