Introduction to Programming with Clojure

  • Why Clojure?
  • What is Clojure good at?
  • What does Clojure look like?
    • Comments
  • What is a REPL?
  • REPL in action

Why Clojure?

If you’ve never programmed before, you may not know that there are many languages to choose from. Some of the other languages you might have heard of (or will hear of!) are C, JavaScript, Python, and Java.

So why are we teaching Clojure? Although it’s not as popular as any of those languages, we’re using Clojure because of three qualities it has that make it an ideal first language to learn–or a great language to learn in addition to others you might already know:

Clojure is simple

Clojure is simple. That’s not to say it’s not powerful; it is. The number of concepts you have to know to program in Clojure is very small, however, and easy to grasp. Clojure grows with you as you learn it, and you can be very productive with a small subset of the language.

Clojure is all-purpose

Clojure is all-purpose. Some languages have a specific focus. JavaScript, for example, was traditionally used only in web pages (although that’s changed somewhat). Objective-C is used mainly for iPhone apps. We’re going to make a drawing application today, but you can use Clojure for any sort of application easily.

Clojure is fun

Clojure is fun. That’s a matter of opinion, of course, but we think it holds true. I hope that during this course you experience the joy of seeing a Clojure program come together and do something powerful and surprising.

What is Clojure good at?

So, we said Clojure is all-purpose, and it is. That doesn’t mean it doesn’t have strong suits, though.

Data processing

Clojure is known for being good at data processing. That’s because it has a good set of data structures–that is, it has several built-in ways to represent data that are easy to use and powerful.


Clojure is known for its concurrency. Think about writing instructions for four of your friends about how to assemble a treehouse, but instead of writing them so one step is done at a time, each of your friends does part of the job. Then, they coordinate at the right time to assemble those parts into bigger parts, and they do this over and over again until the end, when it all comes together. Those instructions would be really complicated and hard to write–and probably hard to read, too. Clojure gives us some easy ways to write these sorts of instructions for computers.


Clojure also works well for building drawing applications with Quil, which is what we’re going to do together.

What does Clojure look like?

(print-str "Hello, World!")
(+ 3 4)
(forward :trinity 40)


Notice the parentheses. Parentheses enclose instructions to the computer in Clojure. A left parenthesis is the start of the instruction, and a matching right parenthesis is the end of enclosing instruction. Normally, Clojure code has a lot of nested parentheses, or in other words, nested enclosing instructions.


Next to the parentheses, we see the instructions to the computer. That instruction is normally what we call a function. The functions do all the hard work in Clojure. print-str, + and forward are all functions. When these functions get run, they return some type of value. Clojure functions always return a value.


Many functions take in arguments–which are everything else inside the enclosing parentheses after the function–. print-str takes “Hello, World!” and returns a string. + takes 3 and 4, adds them, and returns 7. forward takes :trinity and 40, moves a turtle by 40 and returns the result.


When we write code, we try to make it as clear as possible. Doing so is a huge advantage because our code gets read by others (oftentimes more so than by us!), or we come back to our own code to read it later, by which point we may have forgotten each exact detail of the code. One way that we can clarify our code is annotating it with comments. Comments are notes that we add to code, for our own sake, that the computer ignores.

In Clojure, comments can be started with a semicolon. Everything after a semicolon until the end of that line is a comment that gets ignored by the computer. Only one semicolon is necessary, but sometimes you see two semicolons in a row, depending on stylistic tastes.

Reference: Comment

;; example functions from a previous slide
(print-str "Hello, World!")  ; a well-known hello world
(+ 3 4)                      ; why not 3 + 4? figure out later

What is a REPL?

“REPL” stands for “Read-Eval-Print-Loop,” which still doesn’t make a ton of sense without context. Many programming languages, including Clojure, have a way to execute code interactively so you get instant feedback. In other words, the code is read, then it is evaluated, then the result is printed, and you begin again–thus, a loop.

Read, Eval, Print, Loop

Nightcode's repl

REPL in action

Nightcode InstaREPL

To interact with Clojure, we can use InstaREPL feature of Nightcode. It’s a nice way to play with Clojure interactively.

Using the REPL

Nightcode has a project setting aware REPL on a bottom pane. When “Run with REPL” button gets clicked, this REPL starts.

Alternatively, we can start REPL using leiningen on a terminal (without Nightcode). On a terminal, type lein repl, then, REPL starts. If we hit lein repl command within the project directory (folder), it sees the project setting.

Evaluate program and line

Nightcode also lets us evaluate an entire file (program) or line(s). On Nightcode, after REPL has started, “Realod File” and “Reload Selection” works.

EXERCISE 1: Try Nightcode InstaREPL

  1. Start Nightcode
  2. Import myproject
    (which you created while testing leiningen setup)
  3. Open core.clj
    (myproject -> src -> myproject -> core.clj
  4. Click InstREPL button
  5. Type the Clojure functions below and see what happens
(print-str "Hello, World!")
(print-str "Hello, World!" " " "from Clojure")
(+ 3 4)
(- 3 4)
(* 3 4)

Make sure you type the lines exactly as you see them above, taking care to put the parentheses in the right locations.

EXERCISE 2: Evaluate file and line - Part 1

  • Open the file welcometoclojurebridge/src/clojurebridge_turtle/walk.clj
  • Evaluate the entire file by hitting “Run with REPL” followed by “Reload File”
  • See what happens
  • Type (forward 40) on the bottom line of walk.clj in the editor. Evaluate this line by selecting line and hitting “Reload Selection”
  • See what happens

(Continue on EXERCISE 3)

EXERCISE 3: Evaluate file and line - Part 2

(Suppose EXERCISE 2 is done)

  • Type (right 90) and “enter” in the REPL pane (bottom) Run with REPL pane
  • See what happens to the turtle
  • Take a look Turtles App API and How To Walk Turtles [section 1 and 2], and try more commands to walk your turtle

EXERCISE 4: Look at Clojure docs

  • In the bottom REPL pane, try to look up the documentation for a function you have used
  • You can use the (doc function-name) command to do this
  • Try (doc +) and (doc forward) on the REPL
  • Try other functions we used so far, for example, -, *, or doc

Return to the first slide, or go to the curriculum outline.