[previous] [up] [next]     [contents]
Next: Programs are Function and Up: NumbersExpressions, Simple Programs Previous: Errors

Designing Programs

Contracts are specified in the following format:

;; program : input1 input2 ... -> output

but DrScheme cannot check the format of your contracts. In Scheme, a semi-colon ; starts a comment that extends to the next linefeed (i.e., the end of the line if the program text is not automatically line-wrapped). DrScheme ignores anything between the first semi-colon in a line and the end of the line.

DrScheme can insert semi-colons at the start of lines for you. Select all of the lines to be ``commented out'' and select the Scheme|Comment Out menu item. Leading semi-colons are removed from all selected lines by the Scheme|Uncomment menu item.

You can also write a contract inside a text box. To insert a text box into your program, choose the Edit|Insert Text Box menu item. It inserts a text box wherever the caret is sitting.

Beware! If you use text boxes to enter contracts, DrScheme saves your program file in a special format, instead of the normal text format. Only DrScheme can read this special format.
Unlike comments, a text box is not ignored by DrScheme. Instead, it acts like a constant expression (just like a number), so DrScheme shows contract text boxes at the top of the interactions window when Execute is hit. (DrScheme does not look inside the text box, so don't put definitions or test expressions there.) Many programmers find these contracts in the interactions window useful as a summary of the programs in the definitions window. If you prefer, insert a single semi-colon before a text box (making it a comment) to prevent it from appearing in the interactions window.

For the testing phase, a useful trick is to move the commented-out examples below the program definition, and then uncomment them, leaving the = intact between the test expression and its result. Then, when you click Execute DrScheme will run all the tests, printing the actual result, a = (read as ``should be equal to''), and the expected result. For example,

(define (plus-10 n)
  (+ n 10)) 
;; Tests: 
(plus-ten 4) = 10 
(plus-ten -4) = -6 ;; oops! 

produces the output
10
= 
10 
6 
= 
-6 

in the interactions window when Execute is clicked. We can quickly see in the output that 6 is not -6, so either the program or the test case must be wrong.

Test cases written as above must follow the program being tested. However, students should always create the test cases as examples, before the program is written.


[previous] [up] [next]     [contents]
Next: Programs are Function and Up: NumbersExpressions, Simple Programs Previous: Errors

PLT