Syntax checking

Once you've read over your program carefully (your partner, if you have one for this assignment, can be incredibly helpful because your partner won't overlook the same things you do), and fixed whatever errors you found, it's time to give the computer its turn to check that your program is syntactically legal. This step is necessary in any language; fortunately, it's relatively easy in DrScheme. Assuming you've written the program and associated comments in the definitions window, simply use the "Check Syntax" button and see what happens. If (as is highly likely) you get a warning or error message, figure out what caused it, fix it, and try again until you get no warning or error messages.

For some reason, many students are reluctant to actually read error messages. They click the "Check Syntax" button, get an error message, and start changing the program randomly in hopes that the result will pass a syntax check. Don't do this! Making random changes to your program is more likely to make it worse than better.

Don't try to fix a syntax error until you know why it is an error!

When you use "Check Syntax", DrScheme will paint various parts of your program different colors: black for predefined functions, bold-face black for cond, define, and a few other special words, blue for functions you've defined, green for variables you've defined, and (perhaps most importantly) red for words you use but haven't defined anywhere. If you see red in your code, even though "Check Syntax" didn't print an error message, there's probably something wrong. Most likely, it means you mis-spelled a name. Scheme (like most modern computer languages) cares about punctuation, spelling, and capitalization, so it thinks "MyFunction", "myfunction", and "my-function" are three unrelated names. Even worse, it thinks "my function" is two words, "my" and "function", neither of which has any connection to the other three unrelated names.

Once you've done a "Check Syntax", you'll also find another neat feature of DrScheme: when you hold the mouse over any word that you've defined, you see an arrow to where you defined it; when you hold the mouse over the defining appearance of a word, you see arrows to all the places that that definition is used. If any of these arrows point to a different place than you were expecting, figure out why; it may indicate an error in your program (most likely incorrectly-nested parentheses).

Another useful syntax-checking feature of DrScheme is the "Reindent All" command on the "Scheme" menu. This command tells DrScheme to change the indentation of all the lines of code in the definitions window to accurately reflect the nesting of parentheses. If you've written several arguments to the same function on separate lines, they should be lined up with one another vertically. Likewise, if the question and answer of a "cond" clause are on separate lines, they should be lined up with one another vertically, and all of the clauses should be lined up with one another vertically. If things are not indented the way you expect, it indicates that you have some misplaced parentheses. For example,

(define (interest-rate balance)
   (cond [(< balance 500.00) 0]
         [(< balance 1000.00 0.03)
          [(< balance 5000.00) 0.04]]
         [else 0.05]))
The warning sign here is that what should be four clauses of the "cond" aren't lined up with one another: the third one is farther to the right than expected. Of course, you can delete a space to make it line up, but that doesn't change the fact that the program is incorrectly structured. In this case, the line is farther right than expected, so your first impulse might be to add a right-bracket to the previous line:

(define (interest-rate balance)
   (cond [(< balance 500.00) 0]
         [(< balance 1000.00 0.03)]
          [(< balance 5000.00) 0.04]]
         [else 0.05]))
But now when you "Check Syntax", you get the error message "Clause is not in question-answer format".
(define (interest-rate balance)
   (cond  [(< balance 500.00) 0]
 [(< balance 1000.00 0.03)]
  [(< balance 5000.00) 0.04]]
 [else 0.05]))
The trouble is, you didn't figure out why the error occurred before fixing it. The real problem this time isn't a missing parenthesis at the end of the line, it's a missing parenthesis in the middle of the line:
(define (interest-rate balance)
   (cond [(< balance 500.00) 0]
         [(< balance 1000.00) 0.03)
          [(< balance 5000.00) 0.04]]
         [else 0.05]))
Of course, now that you've fixed this, you get the error message "Parenthesis does not match":
(define (interest-rate balance)
   (cond [(< balance 500.00) 0]
         [(< balance 1000.00) 0.03)
          [(< balance 5000.00) 0.04]]
         [else 0.05]))
which is simply because, now that the parentheses and brackets are in the correct places, the marked one should be a square bracket to match the square bracket at the beginning of the clause.
(define (interest-rate balance)
   (cond [(< balance 500.00) 0]
         [(< balance 1000.00) 0.03]
          [(< balance 5000.00) 0.04]]
         [else 0.05]))
Another "Reindent All" to make sure things are correctly nested, and we get
(define (interest-rate balance)
   (cond [(< balance 500.00) 0]
         [(< balance 1000.00) 0.03]
         [(< balance 5000.00) 0.04]]
         [else 0.05]))
which is correct.
Last modified: Wed May 31 14:06:43 EDT 2000
Stephen Bloch / sbloch@adelphi.edu