Professor Matthias Felleisen is one of the original authors of the Racket language and a co-author of The Little Schemer and How to Design Programs. In 1995, Felleisen launched the TeachScheme! Project, reaching out to high schools with a radically novel computing curriculum. Felleisen is the 2012 recipient of the ACM Special Interest Group on Programming Languages (SIGPLAN) Lifetime Achievement Award as well as ACM's 2009 Karl V. Karlstrom Outstanding Educator Award. He is currently a Trustee Professor in the College of Computer and Information Science at Northeastern University in Boston, Massachusetts.
Conrad Barski has an M.D. from the University of Miami and nearly 20 years of programming experience. The author of Land of Lisp, Barski is also an avid cartoonist, having created the popular alien Lisp mascot and many graphical tutorials.
David Van Horn is a research professor who has programmed in Racket and Scheme for over a decade.
Eight students of Northeastern University contributed to Realm of Racket: Forrest Bice, Rose DeMaio, Spencer Florence, Feng-Yun Mimi Lin, Scott Lindeman, Nicole Nussbaum, Eric Peterson, and Ryan Plessner.
Chapter 1. (Getting Started)
You need a Racket before you can launch a rocket, so the first thing you need to learn is how to download and install DrRacket. Once you have it on your computer, you will learn how to experiment. Racket is all about experimenting with expressions and creating your programs from these experiments. After some quick demonstrations, youll be ready to write a game!
1.1 Readying Racket
Racket is a programming language. In principle, the Racket compiler is all you need to write Racket programs. You couldand die-hard Lispers wouldlaunch the Racket compiler in an interactive mode, type in the program, and voil, youd have a running program. Or youd get an error message saying that something isnt quite right, and youd have to retype a part of your program. After a while, this gets bothersome. Just as you need a comfortable seat to work, you need a convenient software seat to develop your programs, to experiment with pieces, to run your test suites, and to explore the partially finished game. We call this place a program development environment (PDE) ; others refer to it as an interactive development environment (IDE) .
DrRacketpronounced Doctor Racketis the PDE for Racket, and it is bundled with the Racket programming language. The original Racketeersthe people who created and use DrRacketwanted to have a PDE where everyoneyoung children, old Lispers, and regular programmerscould quickly feel comfortable. Therefore, DrRacket is designed in such a way that you can immediately experiment with expressions and program fragments. You can edit programs and run them. You can write tests for your program and check them. And using DrRacket is quite easy.
So lets get DrRacket. Point your browser to racket-lang.org,
and near the top-right area of the page, you will see a download icon. Click the icon, choose your platform, and download Racket from any of the sites that show up.
Racket can run on Windows, Mac, and *nix systems. For Windows and Macs, a software installer takes care of everything that needs to happen. If youre on a *nix box, you are already a hero and dont need instructions. In the end, you will have a folder with several applications. Launch DrRacket as appropriate for your platform: by clicking, from a shell, or whatever.
Note
#|
In this folder, you can see another folder called collects and within that folder, you can find realm . There, you can access all the source code for all the games in this book. We encourage you to open these files in DrRacket and to experiment with the code.
|#
Among other things, Racket is a programming language for making programming languages. Because of that, Racket comes in many flavors. Some flavors are for beginners; they are called teaching languages . Others are for writing small shell scripts or large applications. A third kind is for old people who wish to program in long-gone languages. And there are many more flavors beyond this short list. As far as this book is concerned, however, we will show you just one flavor: plain Racket.
You choose which Racket flavor you want to use with these four steps:
Select the Language menu of DrRacket.
Select the Choose Language... menu.
Enable the option The Racket Language.
Click OK and then click Run, the little green go icon.
Now take a closer look at DrRacket. You should see these primary pieces:
Some buttons, including Run and Stop
A definitions panel where you see the text #lang racket
, which means you are using the Racket programming language
An interactions panel labeled Welcome to DrRacket with another line of text that says your chosen language is racket and a third line with just one symbol: >
1.2 Interacting with Racket
DrRacket displays the >
prompt in the interactions panel. The prompt signals that DrRacket expects you to enter an expression, say (+ 1 1)
. DrRacket reads this expression, evaluates it, prints the result, and then displays its prompt again. Old Lispers call this mechanism a read-eval-print loop , but we are sticking with the Racketeer term interactions panel .
Go ahead. Enter an expression. Use (+ 1 1)
if you cant think of anything better. Here is what happens:
> (+ 1 1)2
As soon as you hit the Enter key, DrRacket becomes active and prints the result. Lets try another expression:
> (+ 3 (* 2 4))11
The interactions panel works! Hooray! You type expressions and Racket evaluates them. Experiment some morethats what the interactions panel is for. You could, for example, play with expressions like these:
> (sqrt 9)3> (sqrt -9)0+3i
Yes, Racket knows about complex numbers. How about this:
> (+ 1 2 3 4 5 6 7 8 9 0)45
Calling +
on many numbers actually works. And so do nested expressions:
> (sqrt (+ (sqr 3) (sqr 4)))5
Lists are a pleasure, too:
> '((1 3 5 7 9) (2 4 6 8 0))'((1 3 5 7 9) (2 4 6 8 0))
They come back out just as you put them in, because there is nothing to evaluate with lists.
How about expressions as nested lists?
> '(sqrt (+ (sqr 3) (sqr 4)))'(sqrt (+ (sqr 3) (sqr 4)))
Okay, enough of that. We need to move on to the definitions panel.
Take a look at the sketch below. We entered (+ 3 (* 2 4))
into the definitions panel, hit Enter, and nothing happened. While the interactions panel is for experimenting with expressions, the definitions panel is where you record your code.
To make something happen, click the Run button. DrRacket will evaluate the expression and then print the result 11
in the interactions panel. You could also enter this in the definitions panel:
'(hello world)
Click Run, and youll see '(hello world)
in the interactions panel. DrRacket does all the printing for you; theres no need for you to specify such mundane things. Also, note that your cursor is back in the interactions panel. You can now enter expressions there again and experiment some more.