Introduction to Using DrScheme

Your First Scheme Statements

When starting DrScheme for the first time, choose "PLT/Textual (MzScheme, includes R5RS)" as your Language. This will ensure that the full version of Scheme is available in DrScheme. This corresponds to the "Full Scheme" language option used in older DrScheme versions such as v101. You may need to exit DrScheme and then restart it to get your language choice activated. Now you are set to process the full Scheme language.

You will see a pair of windows. The bottom window says "Welcome to DrScheme." That is the interactive window. This is where you can test statements to see what they will do. Let's test one. Type

(+ 3 4)
This should add 3 to 4. The result of this expression is the integer value 7.

Now let's get a feel for how Scheme couples a value with the type of the value. Type:

(define x 3)
x
(define x (list 3 4 5))
x
(sqrt 3)
(sqrt -3)
(/ 4 3) 
(+ 2 (/ 4 3))

As you can see, Scheme keeps track of the type of the variable for you. It knows about integers, lists (delimited by parentheses), reals, imaginary numbers (like the square root of -3) and rationals (exact fractions). And it has a lot more types than those.

Now let's do a simple function in the top window (the definitions window).
Enter

(define (average a b c) (/ (+ a b c) 3) )
and hit run to compile it.

To test your function, type (in the interaction window):

(average 3 5 7) 

Now let's play with more interesting examples. Load example1.scm from ~cs538-1/public/scheme into the main window (using the File/Open command at the top of DrScheme). It looks like:

(define (plus1 x) (+ x 1))
(define (append L1 L2)
  (if (null? L1)
      L2
      (cons (car L1) (append (cdr L1) L2))
      ))
(define (rev L)
  (if (null? L)
      L
      (append (rev (cdr L)) (list (car L)))
      )
  )
(define (revall L)
  (if (null? L)
      L
      (let ((E (if (list? (car L))
		   (revall (car L))
		   (car L)
		   )))
	(append (revall (cdr L)) (list E))
	)
      ))

To see how DrScheme helps you find references, click on check syntax. Now as you move the mouse around, you will notice that DrScheme shows you an arrow pointing to where the symbol was defined. Mismatched parentheses is a very common problem, and clicking to the left of a left paren or to the right of a right paren (in either window) will show you the portion of the program the paren encloses.

Finding Bugs

Some bugs are easier to find than others. If you click on run it will check the syntax of your program and run it. Many bugs escape detection by the syntax checker, especially since Scheme is dynamically typed (type errors appear at run-time rather than compile time).

Then, of course, there are the nasty bugs where the machine does what you asked for (rather than what you wanted). The best way to find those misunderstandings is to watch what happens each time you enter or exit a function (since Scheme functions are often recursive, a single call may trigger a flood of simpler calls, leading to a final result). To watch the evaluation of a function, you can use trace. Enter

(require (lib "trace.ss"))
to the top of the program (in the definitions window), then push run. Now type (in the interactive window)
(trace revall)
(revall '((1 2) (3 4) 5 6))
When you see each entry and exit, it is easier to see how Scheme arrives at it's answer.

We can also time the execution:

(time (revall '((10 9 8) (7 (6 5 4)) 3 (2 1))))

To turn off tracing of a function f, enter

(untrace f)
To load a file containing function definitions that we want to use without seeing (like a #include in C) use
(load "file name")
where "file name" is the path name to a file (e.g., "~cs538-1/public/scheme/example1.scm").

DrScheme has extensive built-in documentation. Try the help/Welcome to DrScheme/Take a Tour button for a more complete introduction to the capabilities of DrScheme.


Updated Thu Mar 13 13:23:10 CDT 2008