For the second portion of the course, we will be using the Racket language (instead of the OCaml language) and the DrRacket programming system (instead of VS Code). This document describes basic installation and usage steps sufficient for doing your homework.
The main website for Racket-related things is http://racket-lang.org. See in particular The Racket Guide, and also The Racket reference for more detailed documentation of the language.
As with OCaml, we ask that you generally try to confine yourself to the parts of the language that we cover in class or explicitly include on homeworks. Racket in particular has some very expansive features and libraries, so the reason for this policy is first and foremost to prevent confusion and disorientation. If you are in doubt about whether some particular language feature or library is OK to use or not, please ask.
No more custom IDE! (e.g. VSCode) No more grungy old text editor! (e.g. vim or emacs) The Doctor is in town: DrRacket! And they've prescribed an all-in-one environment to make your life as a student simpler.
Download DrRacket and follow the installation instructions
The first time you use DrRacket, copy this line into the file:
#lang racket
Now, click the Run button near the top, and then confirm that the bottom left says, "Determine language from source." If not, click on it to choose this option. DrRacket should remember this choice from here on out.
As we learned about in the OCaml "Modules" lecture, each language has to establish some kind of module system to manage multiple files. This is true of Racket as well. However, since we won't be studying the Racket module system, our goal is mostly to get it out of the way.
In fact, the situation in Racket is more complicated than that. Racket is designed to encourage it's users to develop small programming languages for specialized tasks. These languages are often called "Domain-Specific Languages" (DSLs). These are really cool. Your instructor, Gilbert has done research on these. However, we want to remove this complexity in 341.
For the homeworks, you will have a "homework solution" file and a "homework tests" file. Both will start with the first line
#lang racket
This starting line tells Racket that you want to use the base language for Racket, not some custom DSL. (It's fine to have comments before this line.)
The second line will vary...
In your homework solution file, the second line will be
(provide (all-defined-out))
This line works around Racket's module system. Each file in Racket is its own module, but will hide all of its definitions by default. We want the opposite behavior for the class. The above line ensures that all definitions are visible when we require the solution file from another file (i.e. the test file)
In your homework test file, the second line will be
(require "hw_solution.rkt")
Both files need to be in the same directory/folder on your computer. This line will pull all of the definitions from your solution file into the test file for testing. Note: you do not need a (provide (all-defined-out)) line in your test file.
For the most part, DrRacket is an easy-to-use system with lots of documentation. However, here are a few specific notes that will help you use it most effectively.
If you click Run the currently displayed file will execute via the REPL. A REPL command-line will appear in a pane below the source code file. This REPL window will show any console output from the execution, and then provide a line where you can type interactive commands.
If you then click Run again, the REPL will be shut down and a new REPL instance will be launched to run your file in. This will generally work to reduce confusion.
If you want to save any commands or text from the REPL window, you must do so before you click Run. Otherwise, the REPL will be replaced and all your work will be lost.
If you have a "solution" file and a "testing" file, and you want to run your tests, you need to select the tab for the testing file before you click click Run. If you've included (require ...) as your second line (see above) then this will also run your solution file in the process.
If you get stuck in an infinite loop, you can always click the Stop button to terminate your REPL process.
If you press Alt-P (windows) in the bottom REPL pane of DrRacket, it will start cycling through recently issued REPL commands. This is similar to pressing "up arrow" in utop or most command line terminals. If you are on Mac, you can get the same behavior by pressing Esc, and then "p"
You can ask DrRacket to apply "standard indentation" to a line in DrRacket by pressing the tab key. If you highlight multiple lines and press tab, then all of those lines will be reformatted together. Using this feature will very quickly ensure your program has acceptable formatting style, at the expense of letting you have a say in what that is.
If you type ) in DrRacket, but the matching parentheses is a [, then DrRacket will automatically replace the ) with a ]