Part 1 — A complete beginner's guide to Computer Programming with Clojure.
Part 1 is a simple introduction to the world of computer coding. A number of parts will follow and each should be considered like the chapter of a Book. In Part 2, you will be introduced to a free Virtual Machine with all the necessary resources pre-installed.
I often refer to a computer, or computing enabled device such as a smartphone, as the ‘Machine’. In other words, any machine capable of being programmed (Not your washing machine). I first began to try and understand the ‘Machine’ in 1982. At this time, Computer Science, or as it was known at the time, Computer Studies, was the ‘new kid’ in school. At the time, I saved up all my pennies from paper-rounds and other part-time jobs to purchase my very first Machine, a Sinclair ZX81. At this time, the programming language to know was BASIC. BASIC stood for ‘Beginners All-Purpose Symbolic Instruction Code.’ Compared to my ZX81, the school computer was a highly advanced device built by a local company called Research Machines. It had a 5 and a quarter inch floppy disk and 4K RAM; wealthy schools had BBC Micros or Apples! At this time, the Processor or CPU (central processing unit) was more than likely a Z80 processor. To get the most out of this processor (especially with only 1K of RAM), you needed to know low-level Assembly language. This meant injecting your BASIC code with Peek and Poke commands — necessary to force in Assembly mnemonics and get the most out of the processor. Nevertheless, because of inherent hardware limitations, coding on these old machines ensured you learned to code efficiently and strictly.
My journey with the Machine continued and in 1989 I went to night school at Oxford University to learn C programming. Later, that same year, I got a job as a computer network technician at the Oxford University Computing Services department (OUCS). Here Computer networking was fairly new, with little available literature and certainly no internet to interrogate and look stuff up. During this time, at least in the world of computer networking and communication, the RS232 interface was King (USB didn’t exist). At Oxford, the primary computer network was distributed via telephone wires at a breakneck speed of 9600 baud. In addition, we had to work everything out by conducting experiments and reading product literature (often directly from the RS Components catalog). Over time, the Networks evolved and became a rather confusing mishmash of topologies. Departments and Academics often had their favorite topology. For example, Apple-Talk on one network and another utilizing IBM token ring over coax cable. This often meant installing and configuring Network Bridges — as all wanted access to OUCS services. To assist my networking and computer endeavors, OUCS provided free courses, as well as access to academic experts over a lunch-time beer. This allowed me to learn other operating systems such as VAX/VMS and Unix (prescient considering the impact of Linux). I engaged in many more courses, including some as obscure as Fortran77!
All this reminiscing has a point. You see, at this time (pre-2000), to be considered an ‘IT Expert’ you needed to code (write computer programs). In fact, simple tasks needed some sort of coding and understanding of low-level infrastructure. For instance, at the request of the Academics, I was required to retrieve documents from a digital library located in the USA. This involved setting up a connection via a specialized communication program called ‘Kermit’ and hoping, after at least 24 hours, that the equivalent of a modern-day pdf would eventually arrive (and intact). Interestingly, also during this time, disagreements over perceived facts weren’t solved by a Wikipedia lookup. Instead, a fist-fight was the preferred option — Thank goodness for Wikipedia!
Now everyone is an ‘IT Expert.’ To be an IT expert, all you need to do is a few vanity courses and, perhaps, a Master's degree. There are now more ‘IT Experts’ than those that can actually code. Nevertheless, you will never really understand the Machine unless you can talk to the machine. To talk to the Machine, you need to know how to code.
Coding aka Programming
All computer programs can be built out of just three elements, sequence, selection, and repetition. Sequence refers to a sequence of instructions that follow a specific order. For example:
2. Get out of bed
4. Get dressed
5. Drink Coffee
6. Leave for work
The important thing about Sequence is the order. You wouldn’t want to Shower before you woke-up?
Selection refers to choice:
If raining, take umbrella. Else, wear baseball cap. In our programming metaphor, we would place this specific Selection in between 5. and 6. above.
Repetition means to keep doing an action until a certain condition exists. To continue with our metaphor, we will continue to drink coffee until we are fully awake.
Now, our ‘Morning Routine’ program looks like so:
2. Get out of bed
4. Get dressed
5. While not fully awake:
6. Drink Coffee
7. Repeat until fully awake
8. If raining:
9. Select Umbrella
11. Select Baseball Cap
12. Leave for work
The While statement in line 5 is a type of loop that continues until a condition is met. Once you are fully awake, you will automatically continue to line 8. Once you get to line 12, the program finishes. This simple metaphor is the essence of coding. For any given programming problem, begin by writing out what the program should achieve as a simple statement or paragraph. Next, write out the logic using sequence, selection, and repetition. You will now have a working framework, or plan, with which to tackle your coding problem.
The Clojure language was initially released in 2007. Clojure is a modern dialect of an old language called LISP. For many reasons, which I will not go into because to do so would require a whole other book (or books), LISP became indispensable in early AI (Artificial Intelligence) research. Like all LISP-like languages, Clojure code is written as S-expressions. An S-expression is a simple structure:
(function argument argument … )
For example, to add two numbers:
(+ 2 3)
The + represents an in-built function for addition. The numbers 2 and 3 are the arguments. For mathematics, this is known as Polish notation. So, (+ 2 3) is the equivalent of 2 + 3. Therefore, x > 2 (x is greater than 2) would be (> x 2). Clojure allows you to test these conditions. For example:
(> 3 2) will evaluate to true. See the examples below:
(> 3 2)
=> true (< 3 2)
Both true and false are displayed automatically i.e. as soon as you hit the return key.
The other reason for using Clojure over other programming languages is Abstraction. Clojure is highly Abstract. A programming language with a high level of Abstraction can solve the same problem with less code than a less abstract language. For example, go to this page on the Rosetta Code website:
Scroll down and compare the Clojure code listing to other languages such as Java. You should notice a distinct difference in the amount of written code required to solve the same problem. So, the more abstract a language is, the less code needs to be written.
Concurrency, or more importantly, the ability to process and execute multiple program threads is another Clojure strength. Concurrency is a deep subject and will be covered later on in the chapter about Atoms. For now, understand that modern computer processors have multiple cores. This allows for many ‘tasks’ to run at the same time. In short, Clojure and its ability to create immutable data structures (immutable means cannot change) allow you to safely manage these threads. Note, a thread is simply just a subprogram, and these subprograms need to be managed in a way to prevent both errors and unexpected, or inaccurate, results.
Portability, the ability to work across a variety of machines and operating systems. Clojure is written for the Java Virtual Machine. This Java Virtual Machine, known as JVM, is a simple virtual computer created out of software with the sole purpose of running Java Bytecode. JVM is free and often comes pre-installed on your computer. Otherwise, it is free to download and install from java.com. The JVM tagline is, ‘write once, run anywhere.’ This means you can write a program capable of running on a variety of platforms e.g. Windows, Linux, OSX, Android, etc. Java is the original programming language specifically created to run on the JVM. The great thing about the JVM is that it only runs Java Bytecode. Given this fact, it means that any computer language capable of being compiled into Java Bytecode can run on the JVM. Hence, Clojure (and many other programming languages).
Languages such as Clojure have a rich heritage in its predecessor LISP. LISP was probably the first AI language that could run in a REPL. REPL stands for Read Evaluate Print Loop. We will take a closer look at the REPL in Part 3. The REPL gives the programmer a huge advantage over traditional non-REPL languages. These non-REPL languages must be written, then compiled, then executed (run). If there is a problem with the program, it must be stopped. However, a program running in a REPL can be changed without stopping the program. In other words, the code can be altered while running. This explains why, at one time, LISP was the language of choice for Space Exploration. For example, Voyager, DeepSpace 1, and Mars Pathfinder. You really don’t want to stop a program on a space vehicle millions of miles from Earth!
For more about LISP and its involvement with the space program, check out this link:
Lisping at JPL
Copyright (c) 2002 by Ron Garret (f.k.a. Erann Gat), all rights reserved. This is the story of the rise and fall of…
This first Post introduced the ‘Why’ of coding and a basic ‘How.’ We also covered ‘Why Clojure’ and introduced some programming terms. For instance, S-Expressions, Polish notation, Abstraction, and Concurrency. Computer programming is littered with concepts and their associated jargon. Throughout, I will introduce many more concepts and jargon. Often, I will repeat their explanation with different examples over different posts. At the end of all these posts, you should be able to hold your own in any coding discussion.
For now, recall Clojure is a modern language designed to run on the JVM and uses a specific format (S-Expression) that utilizes Polish Notation for maths. In addition, writing Clojure code involves less typing (Abstract) and is designed to run efficiently on modern multi-core processors (Concurrency).