Racket and DrRacket

CS135 uses Racket as its language of instruction. Racket is not widely used in industry, but is nonetheless very useful for teaching transferable skills that will serve you well professionally. It is also simple and clean enough that we cover computer science content many CS students do not see until later in their CS careers.

DrRacket

The editor (IDE) we use in CS135 is called DrRacket.

The latest version of DrRacket (as of 2023-08-28) is 8.10. You can download it for Windows, MacOS, or Linux for use on your own computer. It is free.

Older versions (version 7 and up) ought to work okay, but if there is a conflict then the version of Racket we support wins.

DrRacket is the “gold standard” for CS135 assignments. If you submit code written under some other system and it does not work in our currently-supported version of DrRacket, then we consider that code incorrect even if it works for you at home.

For more information, see Assignment 0 .

uwaterloo-racket-tools

Once you have installed DrRacket, install the uwaterloo-racket-tools package in DrRacket:

  • In DrRacket, select the File menu, and then Package Manager
  • Click the tab labelled Available from Catalog
  • In the Filter box (1), type uwaterloo-racket-tools , then be patient – it takes some time for the filter to work. Click the package that eventually appears (2), then click Install (3).

Racket Package Manager Racket Package Manager

Unfortunately, the "Failed to insert (an image)" protection doesn't work on at least some Macs. So if you're using a Mac, the test may fail. However, the other utilities this package includes will be useful later in the course.

To test whether you have installed the tools properly:

  1. Restart DrRacket.
  2. Insert the following code in the top pane of the DrRacket window.
(require htdp-trace)

(define/trace (foo x) (+ x 2))

(foo 4)
  1. Click the “Run” button in the upper right corner.
  2. Verify that the bottom pane of the DrRacket window contains Trace Trace (It’s the > and < at the beginning of the first two lines that are important.)
  3. Copy and paste an image directly into your code. If the tools are installed correctly, DrRacket will paste a “Failed to insert” comment instead of the image.

While DrRacket allows you to include images in your code, our testing scripts do not. These tools can help you guard against accidently including images in your code (commonly done when copy and pasting from pdfs or from DrRacket’s interactions window – things you probably shouldn’t do anyway).

DrRacket Hints

  • Use the exact filenames given at the top of the assignment under “Files to submit”.
  • Use the Racket language level stated.
  • Check your Basic Test results.
  • Set some preferences (on a Mac, look in the DrRacket menu; on Windows, look in the Edit menu):
    • In “Preferences > Editing > General Editing”
      • Show line numbers (it makes it easier to talk with course staff about your code).
      • Enable “Maxiumum character width guide” to 102 (the maximum line length in our style guide).
      • Enable automatic parentheses, square brackets, and quotes (it makes it easier to keep them balanced).
    • In “Preferences > Warnings”
      • Ask before changing save format (you always want text format; don’t let it change automatically).
  • Do not copy and paste from the Interactions Window into the Definitions window.
  • Do not submit code using the embedded Special Boxes offered by DrRacket, like Comment Boxes.

Non-plaintext Files

It is possible for Racket program’s to be saved into a “non-plaintext file” format called “gracket”. Our marking software cannot handle this format. The tools installed above are supposed to prevent that. If they fail and you submit a non-plaintest file through the MarkUs website, your Basic Test will contain a warning:

Warning Warning

Save your file by going to DrRacket:
File→Save Other→Save Definitions1 as Text
and then submit your file again.

gracket format should not be an issue for you if you have followed instructions and installed the uwaterloo-racket-tools above; these tools protect you from the most common ways students accidentally convert their code to gracket format. Without this package, the following actions can render your Racket files “non-plaintext”:

  • Copy and pasting from the Interactions window
  • Copy and pasting from assignment PDF files
  • Inserting images or comment boxes into your Racket code

Tracing Library

Racket has libraries to trace function calls. This can be very helpful when troubleshooting programming problems and especially recursive functions (M06 and later).

First, make sure you have installed the uwaterloo-racket-tools package as described above. At the top of your program add the following line:

(require htdp-trace) 

Now, to trace a function, change the define in the function definition to define/trace. For example,

;; (count lst) produces the number of elements in lst
(define (count lst) 
  (cond [(empty? lst) 0]
        [else (+ 1 (count (rest lst)))]))

becomes

(require htdp-trace)

;; (count lst) produces the number of elements in lst
(define/trace (count lst)
  (cond [(empty? lst) 0]
        [else (+ 1 (count (rest lst)))]))

After doing this, calls to count will be traced in the Interactions Window.

Important: Before submitting your code to MarkUs, change any define/trace invocations back to define.


  1. There is also a “Save Interactions as Text” that you do not want. ↩︎