Thursday, 12 November 2015

Racket on the Raspberry Pi

Racket is a nice programming language to experiment with -- OK there's a bit of adjustment to the lisp syntax, but once you get that you get access to a wealth of built-in libraries for graphics, animation, UIs and more.



The Racket package in the official Raspian repo doesn't work out of the box, but you can download and compile it yourself. It takes an hour or so, but is not hard to do.

I've tried this on both the newer model 2, and the older version of the Raspberry Pi. It works reasonable well on the newer model, but is frustratingly slow on the older one.

Step 1: Download the latest version from the website

Go to http://download.racket-lang.org/ and choose Unix source + built packages.

You can also download with:
wget http://mirror.racket-lang.org/installers/6.2.1/racket-6.2.1-src-builtpkgs.tgz

Step 2: Compile it from source

Check the version numbers in the commands below. At the time of writing 6.1 was available.

tar zxvf racket-6.1-src-builtpkgs.tgz
cd racket-6.1/src
./configure
make

Finally run:

sudo make install

Step 3: Run DrRacket to try it out

Start up DrRacket, you can find it in the bin directory:

cd ~/racket-6.1/bin
./drracket

Enter the following into the top part of the window (the Definitions) and click Run.

#lang racket
(require pict)

(define (circles n)
  (if (zero? n) empty
      (cons (circle (* n 15)) (circles (sub1 n)))))

(define (squares n)
  (if (zero? n) empty
      (cons (rectangle (* n 15) (* n 15)) (squares (sub1 n)))))

Now run these in the bottom part, the Interactions:

(apply cc-superimpose (squares 8))
(shuffle (append (circles 4) (squares 4)))

DrRacket has great built in help, simply place your cursor a command and hit F1 to read the docs.

Step 4: Check out the games

There are a bunch of games in ~/racket-6.2.1/share/pkgs/realm/ -- from the book Realm of Racket.
The snake game in chapter6 is a good place to start.

Have fun...

1 comment:

  1. Thanks for the post. I tried this on a Pi3 with a later version of racket, and was able to get Dr Racket working.

    ReplyDelete