Racket on the Raspberry Pi

:: Racket, Pi

One of the great features of the Raspberry Pi® is that because it can run a full Linux distribution, it’s an embeddable computer with vast array of programming languages just waiting to be installed; most just an apt-get away. Here are some instructions on installing the Racket language environment. As W1A’s Will1 might say “Racket, yeh cool…what, say again?”. You may never have heard of Racket.

The Racket site describes it as a “programmable programming language” which comes from a background of Lisp and Scheme. This is a pedigree with a long history of providing ways of turning thoughts into computer programs in a very flexible and powerful way.

Why Racket on the Pi? Well, Racket has a long association with the teaching of computing skills (more of that in a future note). Its characteristics make it really good at complementing, or as an alternative to other programming languages which might be considered in the teaching of computing, such as Python, or (god forbid) Java, in England’s new computing National curriculum.

Getting started

At the moment, getting Racket onto the Pi requires some leg-work—it’s only recently that Racket has fully supported ARM-based systems. Also, be aware that large Racket programs (such as the full Racket development environment) stretch the Pi’s resources, much as the built-in Mathematica environment does. So in the end you may want to do exploratory development on a more heavy-duty machine and then move the programs to your Pi, but that’s not to say that you can’t have a lot of fun with it. You can also use GNU emacs and Geiser on the Pi as a great development setup. This note assumes:

  • a Pi model B/B+ running a recent version of Raspbian
  • a couple of hours to spare, most of which you can spend reading a good book while the Pi does things by itself.

Download Racket

From the Racket site, download the ‘Unix source + built packages’ compressed file from the download page and uncompress it. You can do this from a terminal with:

wget http://mirror.racket-lang.org/installers/recent/racket-6.1-src-builtpkgs.tgz
tar xzvf racket-6.1-src-builtpkgs-unix.tgz

You’ll find this results in a racket–6.1 directory. cd to the enclosed src directory and create a directory called ‘build’, and cd into that—this is where you’ll be operating.

cd racket-6.1/src
mkdir build
cd build

Configure, make & install

From inside your build directory

../configure

This will result in a lot of output, as a script tests for all the facilities needed to create the Racket programs; ending in a message

>>> Installation is in-place:
  ../..
Configure with --prefix if you wanted to install somewhere else.
The --prefix option also makes the installed files better conform
to Unix installation conventions. (The configure script will show
you specific installation paths when --prefix is used.)
Alternately, you can simply `mv' the in-place installation after
running `make install'.

Now build the package. At this point, you have about an hour and a half to cook a tasty snack, read that book, or stare in amazement at the events unfolding on your Pi.

make; make install

At the end of this process, cd to the top-level racket–6.1 directory; there will be new sub-directories, including bin .

cd ~/racket-6.1/
ls
bin  collects  doc  etc  include  lib  man  README  share  src

Running

If you run bin/racket and all has gone well, you will see

bin/racket 
Welcome to Racket v6.1.
>

To allow Racket commands to be called more easily you can edit your .bashrc file and add a line at the end.

nano ~/.bashrc

append

PATH="$PATH:$HOME/racket/bin"

then

source ~/.bashrc

also make a symbolic link to the racket directory, to make updates easier

ln -s ~/racket-6.1 ~/racket 

What can I do now?

The > is a prompt; Racket is waiting for a command, type (+ 1 2) and you should see this

> (+ 1 2)
3
> 

Type ctrl-D (hold down the cntrl key and press d) to exit Racket’s command line. You’ve run your first Racket program from its terminal-style application. So, Racket in common with many languages derived from Lisp uses a syntax which may look strange to begin with; expressions are grouped within parentheses, with the first item generally being the operation to perform on the remaining items. Hence

(+ 1 2)

says ‘perform plus on the numbers 1 and 2’, and so

> (+ 1 2 3 4 5)
15
> 

Did you spot what happened there? Racket’s syntax let us sum a set of numbers very naturally. There are other, very important results of this style.

Racket has a large number of libraries, for example to draw graphics, which can be loaded by use of a require. So just as a flavour, lets create an image .png file—_2htdp/image_ is an image-generation library.

Welcome to Racket v6.1.
> (require 2htdp/image) 
> (define my-image (circle 50 "solid" "blue"))
> (save-image my-image "picture.png")
#t
> 

and lo, there’s a png with a circle in it in your filesystem.

A picture of a blue circle

A picture of a blue circle

Going further

This is just a minimal first taste, Racket has some excellent documentation, and there is a fun book ‘Realm of Racket’.

As mentioned before, the main Racket development environment DrRacket is not quite snappy enough on the Pi to avoid some frustration, but you can see what it looks like by running

drracket 

Racket is an excellent language for Pi GPIO-type activity; being very fast for I/O twiddling (see A Racket interface to wiringPi ). In the meantime I can’t recommend anything more inspiring than to watch a video of Emmanuel Schanzer’s Bootstrap project which teaches students to program their own video games using Racket, and ideas from an introductory computing textbook How to Design Programs.

I hope you have fun with all this.