Ed.
Like all languages, Racket has a particular style and standard conventions. Here is a short list to start off with:
Racket
menu contains a Reindent All
item which you can use to reindent the entire file.}
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
)
)
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).
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!