Using Dr. Racket's sdraw Package
Drawing Box-and-Pointer Diagrams in Dr. Racket
In Session 4, we learn to draw
box-and-pointer diagrams
to help us understand Racket lists, including the how
cons
and list
work.
You may want to practice drawing box-and-pointer diagrams. How can you know if your diagrams are correct?
With the sdraw package, Dr. Racket can help by drawing a box-and-pointer diagram for any Racket list, right in the Interactions pane.
If you would like to use sdraw, read on to learn how to install and use the package.
Installing the sdraw Package
sdraw is a third-party package for Dr. Racket, so we have to install it.
Step 1: Select Install Package... on Dr. Racket's File menu:
Step 2: Copy this string:
https://github.com/jackrosenthal/sdraw-racket.git
and paste into the Package Source field of the window that pops up:
Step 3: Press the Install
button. It will take
a few seconds for Dr. Racket to download and install the source
code and documentation for the package.
You are all set.
Using the sdraw Package
To use the sdraw package, we use require
to
import it into a file or interaction. Then we call the
sdraw
function to draw a box-and-pointer diagram for
a list.
For example, to draw the box-and-pointer diagram for the list
'(a (b c) d)
, we can do the following:
(require sdraw) (sdraw '(a (b c) d))
Notice that by default, sdraw draws the empty list explicitly
as a pointer to ()
. If that is okay with you, then you
are set. If you would like to the diagrams with null pointers,
read on.
Customizing sdraw's Output
The sdraw
function accepts a number of keyword
arguments that customize the way it draws its diagrams.
You may have used keyword arguments in Python to customize theend=''
) or to write to a file (file=filehandle
).
To draw box-and-pointer diagrams the way we do in class, with null
pointers in the last cons cell of a list, we need to pass the
#:null-style '/
keyword argument to sdraw
.
For example:
(require sdraw) (sdraw '(a (b c) d) #:null-style '/)
Customizing sdraw's Output Once and For All
If you would like to draw this sort of box-and-pointer diagram
without having to call sdraw
with the extra
parameter every time, download the file
draw-boxes.rkt.
It creates a new version of the function with null pointers as the
default.
Simply place the file in your working folder, and
require
it into Racket instead of the package:
And now you really are all set!
More on the sdraw Package
If you would like to learn more about the sdraw package, and especially more ways to custom the diagrams it draws, visit the package's documentation page.