Programming with
Data Structures and Algorithms

Getting Started

Get Acquainted with DrRacket

A programming environment is a workspace in which programmers create and run programs. Our programming environment, DrRacket, is the work of researchers (many of whom are students) at several universities, including this one.

Task: At a Unix shell prompt, type drracket &.

Note: The & tells the shell to open DrRacket in the background. If you don't include &, DrRacket will run in the foreground. This means you won't be able to use the shell until you close DrRacket. If you ever forget to use &, you can switch to the shell, press Ctrl+z to suspend DrRacket, and then run bg (or simply &) to move the DrRacket process to the background.

A window will appear with a patriotic lambda and a progress bar while DrRacket loads up various extensions. Soon this window will disappear and a large window will replace it. The top half of this large window is called the definitions window, and the bottom half is called the interactions window. When you click on the "Run" button (top right), DrRacket will evaluate everything in the definitions window, and the values computed will appear in the interactions window. You can also type expressions into the interactions window directly and press enter to evaluate them immediately.

Task: Configure the language level. Follow the instructions on the software page.

Task: Now you're ready to try out DrRacket. Type (+ 4 3) into the definitions window and click "Run". You should see the number 7 in the interactions window.

Practice with Arithmetic Expressions

Task: Type the following expressions into DrRacket's interactions window one at a time. Some of them contain invalid syntax. Reformulate them so that DrRacket returns the desired value.

Note that in Racket, arithmetic operators like + and * are not limited to exactly two arguments; in CS 19 Racket they can take more, but not less. Even non-commutative operators like / can take as many arguments as you like. However, it's difficult to read an expression like (/ 5 4 3), so we recommend that you always use / and - with two arguments only.

Editing Tips

As in most text editors, Ctrl+Left and Ctrl+Right move the cursor left and right by a single word. DrRacket also provides another very useful shortcut - Alt+Right and Alt+Left move the cursor by one entire parenthesized expression. Type some expressions with nested parentheses and try it out. can also use Alt+Shift+Left and Alt+Shift+Right to select parenthesized code blocks.

One common annoyance in the interactions window is that when you press enter in the middle of a line, say because you made a mistake a line above and pressed Ctrl+Up, DrRacket will just insert a newline instead of running your code. This is the intended behavior, and is very useful when constructing complex expressions in the interactions window, but it can be bothersome at times. The End key is very useful here, but if you forget and hit enter, Ctrl+z is a much easier way to fix your mistake than hitting backspace a bunch of times.

Comments and Indentation

In general, you want to make your code as legible as possible. Because people have to understand your code, you should add comments in clear English to your code. In Racket you can do this using semicolons. Any text that appears after a semicolon on a line will be a comment and Racket will not evaluate that text. For example, the following two Racket expressions have the same value:

19
19 ; this is the number 19
DrRacket handily colors comments orange so they are easy to identify.

It is considered good style to use two semicolons for explanatory comments and one when commenting out code.

Task: Type the following into DrRacket's definitions window, then add semicolons where necessary to separate the comments from the code.

This is a very simple program.
(+ (* 2 3) It adds the product of two and three
   (* 4 5)) to the product of four and five
and should evaluate to twenty-six!

Sometimes you will want to comment out an entire expression or a block of code at the same time. This can be done quickly in several different ways.

Try using one of these methods to comment out all of your code from the previous task.

Indentation is another important way to make your code easier to understand and help you spot errors. DrRacket automatically indents the next line of code every time you press enter. If a line is indented incorrectly (if you changed the lines above it, for example) you can also press the Tab key with the cursor on that line, and DrRacket will automatically fix the indentation. Alternatively, you can press Ctrl+i to automatically fix the indentation in the entire file.

Task: Copy the following code into the definitions window and edit it so that it is more readable (readable indentation counts on your homework!).

   (define-struct boa (name length food))

   ;; danger-to-rodents? : boa -> boolean
   ;; determines whether a boa eats rats, mice, or gerbils
   (define (danger-to-rodents? aboa) (or (equal? (boa-food aboa) "mice")(equal? (boa-food aboa) "rats")(equal? (boa-food aboa) "gerbils")))

Syntax Checking and Debugging

Task: To experiment with some of DrRacket's more powerful tools, copy the following program into the definitions window. This program has some errors which you will correct later on in the lab.

;; babel : string -> string
;; produces word for hello in the given language
(define (babel lang)
  (cond [(equal? lang "spanish") (+ 4 "hola")]
        [(equal? lagn "french") "bonjour"]
        [(equal? lang "pig-latin") "ellohay"]))

DrRacket provides a built-in syntax checker to easily spot typos and other simple mistakes.

Task: Press the Check Syntax button.

Your program has been colored in purple, green, red, and blue. Built-in operators appear in purple. User-defined identifiers appear in blue. Constants appear in green. Identifiers that DrRacket doesn't recognize appear in red. One misspelled identifier should appear in red.

Move your cursor over the blue symbols. Notice that blue arrows pop up showing where each identifier is defined/used (except in define-structs). This is another tool that can help you locate errors in your programs. Note that Check Syntax does not find more sophisticated errors, such as type errors.

Task: Fix the error you found, and press Check Syntax. Now try running (babel "spanish"). What happens?

DrRacket comes with a more powerful tool, the debugger, to help you find errors like these. Like the syntax checker, the debugger only works on code in the definitions window; to use it, press the Debug button. When you do, some new buttons appear:

You can set breakpoints in your program while in debug mode by hovering the mouse over an open parenthesis. A red dot should appear. Right click (and hold down the right mouse button; this is a bug) on the dot and choose "Pause at this point". The red circle should darken and become permanent. Now when you click Go, execution will pause before evaluating the expression with the dot next to it, allowing you to begin stepping from that point.

Task: Put the line (babel "spanish") at the bottom of the definitions window. Run the debugger, and explore the behavior of the Step, Over, and Out buttons. Look at how the contents of the stack and variable environment change as you move through the program. Find the other error in this program and fix it. Save your work.

If you have any questions about syntax or setting up your program environment, feel free to email cs019tas or come to hours.