Thursday, 25 May 2017

A little bit of Julia

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
function guess()
    print("Enter a guess (1 to 10)")
    a = chomp(readline())
    return a

# 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, or if you need to find out how it works you can look there (see below).


Just Enough Python
An brief introduction that will get you programming quickly


  1. This comment has been removed by the author.

  2. mmm, do we really need yet another language for the RPi,
    Python + numpy etc or Anaconda offers a lot of numerical support. However I will have a look at Julia later.
    The one thing I dislike about Python are the blanks as block structures - it looks as though Julia has that as well.

  3. I guess we don't actually need a new language and Python has the advantage of being more mature and well supported but Julia does look quite good and I think I agree about Python's strict indenting.