Racket Fundamentals

We will be using Racket as the base language this semester. Racket is best installed via the Racket website: https://download.racket-lang.org/.

Select the Racket (not Minimal Racket) distribution for your platform - this will typically be the 64-bit version. If you have a Mac with an M1/M2 chip (i.e. 2021 or newer), you need to download the Apple Silicon 64-bit version. We are using the newest Racket release (Version 8.11) in this class.

Racket/DrRacket are also available on the lab machines that you have access to as part of being a current CS department student.

Installing Racket actually installs Racket, the language, and DrRacket, the integrated development environment (IDE) for Racket. It is strongly recommended that you use DrRacket as your programming environment in this class, as there are elements of the IDE (such as the GUI debugger) which are only available in DrRacket. Former students have tried other editors (notably Visual Studio Code), and come back to DrRacket!

If you have any issues, please ask on Ed.

Coding style

Like all languages, Racket has a particular style and standard conventions. Here is a short list to start off with:

  1. You should let DrRacket indent your code. You can re-indent a line of code at any time by pressing tab. The Racket menu contains a Reindent All item which you can use to reindent the entire file.
  2. Closing parentheses and brackets should not appear on lines by themselves (as you might do with } in Java or C). For example, a procedure to compute the arithmetic mean of two numbers might be written as
    (define (mean x y)
      (/ (+ x y) 2))
    

    or

    (define (mean x y)
      (/ (+ x y)
         2))
    

    but not as

    (define (mean x y)
      (/ (+ x y)
         2
      )
    )
    

Play around with the IDE!

DrRacket is a relatively simple IDE—it is designed for the educational use case in mind, but has a lot of supported features. The top panel is your editor, whereas the bottom panel is where your code prints when you press “Run” in the top right corner. The bottom panel also functions as a REPL (Read-Eval-Print-Loop) similar to interactive versions of other languages (e.g. Python’s REPL).

Testing

A note about how tests work in Racket. Let’s say I have the method add-two from class. Here’s three tests for it:

(define add-two-tests
  (test-suite
   "all add two tests"
   (test-equal? "positive"
             (add-two 3)
            5)
   (test-equal? "negative"
             (add-two -5)
             -3)
   (test-equal? "zero"
               (add-two 0) 2)))

Tests have three main parts: you name a “test suite” using define. Typically use the name of the procedure that you’re testing: in this case, add-two-tests.

The body of that define is a special form called test-suite. It takes a general name (in this case, “all add two tests”) and can take as many other arguments as you give it.

A single test takes the form of test-equal?, test-true, or test-false. So above one test is

 (test-equal? "negative"
             (add-two -5)
             -3)

test-equal? takes three arguments, the name of the test, an input, and then the expected output. It’s your just to build these tests and “instantiate” the general purpose situation (here testing a negative number) with an example (e.g. -5).

If you have questions about ask on Ed!