From Commodore 64 to Clojure

Nick Tyson
7 min readOct 20, 2019

--

I have always interested in computers, a ZX81, ZX Spectrum and the finally, real keys, A Commodore64! I still remember trying to get my head around being able to type ‘print’ and the computer being able to understand it instead of ‘P’ on the Spectrum that brought up Print on the screen.

I really loved it, and 13 year old me really thought he could be writing programs for NASA in no time, or breaking into some secret Government mainframe.

Skip forward a few years and football, girls and booze had taken up most of my free time and the computer ended up gathering dust. Through various jobs I started getting back into computing with 80286 PCs in the mid 1990s. I immediately got excited again, and worked for a couple of years at a small company without any PCs. This was where I learnt about networks and servers, after convincing the owner that building an IP network in each office and connecting them across a WAN was a good thing.

I eventually moved on to larger and larger companies and as my career grew, my direct connection to technology decreased, until reaching almost zero as I become an Enterprise Architect. Fortunately around this time I started reading huge amounts of material about DevOps and fast feedback loops etc. This got me excited in technology again and I wanted to get back into coding to be able to put into practice the ways of working I was so enjoying reading about. After messing a little with Java and Python, I next turned to a LISP language — Clojure thanks to a post on twitter from Gene Kim. I’ve not done an awful lot with it yet, but already the language is chiming with my brain and I’m excited to see where it takes me. At Gene Kim’s request, I include details below of how I got myself setup.

Clojure is a functional programming language that runs on the Java platform. I recommend playing with Clojure and learning how elegantly it solves problems and keeps programmers honest.

ALL THE TOOLS YOU NEED

I have a Mac, but all the tools listed below can run on Windows too, though you won’t use Homebrew. The tools below are all you need to be able to code and run Clojure.

Install Java and Java JDK using the links above. It’s best to reboot after you have installed Java.

Install homebrew using the link above and test to make sure it is installed by typing ‘brew’ in the terminal…

$ brew

…and you will get the following result

To install leiningen, simply go to the terminal and type

$ brew install leiningen

To test the install, type in the terminal, which gives you the following results

$ lein

The simplicity and beauty of Leiningen is that you can run Clojure from here without anything else. Create a new directory, anywhere on your Mac, and move there in the terminal

$ mkdir test
$ cd test
$ pwd
/Users/name/test

Next we can create a small project in this directory by typing

$ lein new app testapp
$ ls
testapp

This has created a new project/directory called testapp. Move into that directory

$ cd testapp

The project template is ready to run already without having to type any code. to run the application type

$ lein run

The output should look like this -

Only 10 mins in and we have ‘Hello World!’ running :-)

LETS GET GRAPHICAL

To make life a little easier I’m using IntelliJ as my Integrated Development Environment (IDE) — a fancy editor if you’re not familiar. Install the community edition using the link above. All the defaults are fine for this install.

Once installed, open IntelliJ and go to preferences | plugins, and search for Clojure or Cursive. The screen capture below shows the plugin installed on my system.

Restart IntelliJ and when it starts, open the project you created earlier

The project will open in IntelliJ and you will be able to open the directories on the left to find the application, as per the screenshot below

Potentially, there may be a message in the right hand pane asking for the location of the Java SDK. Point the application at your earlier install.

If you’ve used an IDE before, you might think you can just run the application. You can do that but you will see in the pain at the bottom that ‘HelloWorld!’ does not appear, though the exit code is 0. This is because the IDE does not know to use Leiningen to run the application. To rectify this, click ‘add configuration in the top right corner, which opens the Run/Debug Configurations window, then choose ‘+’ from the top left corner, and select Leiningen.

Once clicked, change the Name to whatever you wish, I’ve chosen testapp to match the project name, and then in the arguments section type run -main

Click ‘OK’

When you select the core.clj from the directory tree on the right and you see the code in the middle pain. Make sure in the config you just updated, you specify to run with Leiningen

If you now run your code, you will now get the ‘Hello World!’ outputYou’re now ready to code.

REPL

There is one additional item that is very useful REPL (Read Eval Print Loop) Leiningen include REPL, so there is no need to install anything.

From the terminal, navigate to your testapp directory and type

$ lein repl

And the result will be something like the below. In addition, once repl is running, if you were to type ( -main) then the “Hello World!” app will run. See below.

REPL can either be local or a remote server. The real beauty of REPL is that you can run small chunks of code at the command line. So a simple bit of clojure code might be (println "My name is Nick") If you type that at the command line in REPL, it will print My name is Nick

In the same way we added Leiningen, we can also add REPL to IntelliJ so that we can have a command line and an ability to run small bits of code to help with debugging. Within IntelliJ click ‘edit configuration’ in the top right, and choose ‘+’ | ‘Clojure REPL’ | ‘Local’

Give a name of repl and keep all other settings default, and click ok

Where repl appears in the top right corner, click the green arrow to run repl. You can see the output below

In the bottom right box, you now have a command line within repl. So if you type ( -main) in the box, it will run the “Hello World!” app. You can also run command and calculations etc.

Finally, if you have multiple files and wish to switch which file is loaded into repl. Right click and choose repl|Load file into REPL.

I hope this is a useful guide to getting started with Clojure. It gives you everything you need to start coding, debugging and running your Clojure programs. Feel free to ask any questions.

--

--