Julia is available for many platforms but was announced as a new programming language for the Raspberry Pi recently, so I just had to have a play with it.
Julia is designed for numerical computing but works as a general purpose programming language, too. The Raspberry Pi version also has libraries for interacting with the GPIO, SenseHAT and Minecraft.
The main Julia web page is here and the Julia web page for Raspberry Pi is here.
In this blog I’m just going to look at the basics of Julia by writing a simple guessing game program (similar to the one in my Just Enough Python blogs).
First, though you need to install it. This is simple just open a terminal window and type
sudo apt-get update
sudo apt-get install julia
Wait for a while and you are all set. You can run Julia from the command prompt. This will give you an interactive Julia session like this
To get out you have to type control-d.
This is great for experimenting with Julia but I’m not going to dwell on this.
Instead I’ll present a simple program that shows how to use simple input and output, decisions, loops and functions.
Here’s the program
# Simple guessing game
# the computer thinks of a number between 1 and 10
# you have 5 attempts to guess it
#define a function to get a guess
print(“Enter a guess (1 to 10)”)
a = chomp(readline())
# Start here
#Get a random number
number = rand(1:10)
# Convert it into a string
answer = string(number)
# Get a guess from the user
attempt = guess()
# Let the user guess 5 times
for i in (1:5)
if attempt == answer
println(“Correct – well done!”)
println(“Wrong – try again”)
attempt = guess()
print(“The answer was “)
You can run this from the command prompt using the command julia filename.jl (julia programs generally have the extension .jl).
As you can see it is quite straightforward. If you are familiar with Python, or any other language, really, you won’t have too much problem in working out what is going on here.
One difference with Python is that blocks of code inside a function, if statement or loop end with the end keyword. Indentation is not strict, as in Python, but it is, of course, conventional to indent properly. It reminds me of Pascal in this respect.
This is the Julia version of the program in my tutorials Just Enough Python – if you need to find out how it works you can look there (see below).
Back to Which Language for the Raspberry Pi.
Just Enough Python
An brief introduction that will get you programming quickly