Thursday, 20 March 2014

Smalltalk on the Raspberry Pi

Smalltalk is a great language for learning. Everything runs in an integrated environment, so it's quick to try things out; it's simple to grasp the basics and there are lots of visual things included in the environment.

Each Raspberry Pi comes with the Squeak version of the Smalltalk programming language. Well almost, most of what you need it there, but there are a few bits to collect from the internet to complete the package. Run these commands from a terminal shell to get them...

mkdir squeak-downloads; cd squeak-downloads

wget http://ftp.squeak.org/4.1/SqueakV41.sources.gz
gunzip SqueakV41.sources.gz

wget http://ftp.squeak.org/4.4/Squeak-4.4-All-in-One.zip
unzip -j Squeak-4.4-All-in-One.zip "**Squeak4.*.image"
unzip -j Squeak-4.4-All-in-One.zip "**Squeak4.*.changes"

sudo mv SqueakV41.sources /usr/share/squeak
sudo mv Squeak4.*.image /usr/share/squeak/
sudo mv Squeak4.*.changes /usr/share/squeak/

Starting Squeak

Now you can start up Squeak, you'll find it in the programming menu, and open a new Workspace from the Tools menu. This is where you can execute commands with immediate effect.

Type in the line below and execute it by bringing up the right mouse menu, or pressing Ctrl-D (for 'do it').

b := BouncingAtomsMorph new openInWorld.

You should see a panel with some bouncing atoms in it. Now try each line below, running each one in turn:

b setAtomCount.
b startInfection.

When you've had enough you can kill the morph by Alt-Clicking it and choosing the X icon, or type `b delete`

Drawing with a Pen

Now type in the following:

p := Pen new.
p goto: 300@300.
p squareNib: 4.
p down.
p spiral: 200 angle:45.

Select all the lines (Ctrl-A), right click and choose 'do it' from the menu. You should see a nice rainbow spiral.

What just happened? You created a new `Pen` and sent it some messages:
  • to go to a point some way into the middle of the screen
  • that you want a square nib of width 4 pixels
  • to put the pen down ready for drawing
  • to draw a spiral with 200 segments turning 45 degrees at each step.
There's lots more you can do with pens, take a look by typing Pen in the search box at the top of the page and browsing all of its methods. E.g. try: `p filberts: 5 side: 4.`

A few things to notice:
  • messages that take a parameter, e.g. `p goto: 300@300` have a colon after the message name, this style is repeated if you need to supply multiple parameters. 
  • each Smalltalk expression ends in a dot '.' -- don't forget this otherwise Smalltalk will join your lines together and you'll get strange error messages.
  • for more info on Syntax see http://en.wikipedia.org/wiki/Smalltalk#Syntax

Sound?

Sound doesn't work in Squeak without a bit of tweaking. Here's what I tried first:

FMSound bass1 play.

...nothing. But there's a solution here: http://raspberrypi.stackexchange.com/a/13221/12203

What next?

You could try creating some Fractal Trees.

If the above has got you interested, a good place to learn more is the free book: http://squeakbyexample.org/

No comments:

Post a Comment