An Animated Introduction to Clojure – Learn Clojure Programming Basics

This tutorial introduces the programming language, Clojure. Clojure is an awesome functional programming language that runs on Java's Virtual Machine (JVM). It is sometimes called a LISP, which is short for 'LISt Processing'. You'll need to have some...

Apr 10, 2025 - 04:46
 0
An Animated Introduction to Clojure – Learn Clojure Programming Basics

This tutorial introduces the programming language, Clojure. Clojure is an awesome functional programming language that runs on Java's Virtual Machine (JVM). It is sometimes called a LISP, which is short for 'LISt Processing'.

You'll need to have some previous programming experience in another language to get the most out of this tutorial. Clojure is so different from most imperative programming languages that it doesn't really matter where you are coming from as long as you know the basics (variables, if statements, loops, and functions).

If you are looking for a complete beginner's guide to programming, you can find some of my other programming content below.

Here, I will cover functions, data structures, immutability, closures, tail recursion, lazy sequences, macros, and concurrency.

Clojure is a functional programming language. In it, the function is king and data is immutable. This may be a different paradigm than you are used to, but there are some compelling reasons to use it. Clojure's immutability is particularly good for programs that need to take advantage of modern hardware on laptops and mobile phones (multiple processors sharing a single memory).

Clojure runs on the JVM. It can be difficult to set up Clojure because it involves installing Java, Leiningen, and sometimes an editor plugin. To make things easier, I recommend starting with a web-based IDE. There is no easier way to start programming in Clojure than using replit. I recommend using it to write your first programs in Clojure. If you feel up to it, check out these docs to get started on your own machine.

Even if you don't use Clojure every day, learning it will change how you think about programming. It will help you understand recursion, higher-order functions, and data transformations in a new way. These ideas transfer well to other languages like JavaScript, Python, or Rust. In short, learning Clojure will make you a better programmer.

Code Playbacks

This material will not be delivered like traditional online tutorials or video series. Each section includes links to interactive code playbacks that visually animate changes made to a program in a step-by-step manner, helping you understand how it was written.

A code playback shows how a program evolves by replaying all the steps in its development. It has an author-supplied narrative, screenshots, whiteboard-style drawings, and self-grading multiple choice questions to make the learning process more dynamic and interactive.

Here's a short YouTube video explaining how to view a code playback:

Playback Press

Playback Press is a platform for sharing interactive code walkthroughs that I created. I’m Mark, by the way, a professor of computer science. These books provide interactive programming lessons through step-by-step animations, AI tutoring, and quizzes.

If you want to see the full Clojure 'book', you can go here:

An Animated Introduction to Clojure, by Mark Mahoney

I also built Storyteller, the free and open-source tool that powers code playbacks.

AI Tutor

When viewing a code playback, you can ask an AI tutor about the code. It answers questions clearly and patiently, making it a helpful resource for learners. You can also ask the AI tutor to generate new self-grading multiple-choice questions to test your knowledge of what you are learning.

To access the AI tutor and self-grading quizzes, simply create a free account on Playback Press and add the book to your bookshelf.

Table of Contents

Introduction to Clojure

These first few programs show how to print to the screen, perform basic arithmetic, and store some data. Go through each of these now:

This program shows how to use the Java capability that it built into to the JVM.

These programs show some basic data structures in Clojure and how they are immutable.

Hands-On Practice

Problem 1

Write a Clojure program that prompts the user for the length and width of a wooden board in inches. Then display the number of whole square feet that are in the board. For example, if the height is 27 inches and the width is 34 inches, then the number of square feet is 6.375.

Problem 2

Write a program that creates an empty list and use def to store a reference to it called empty-list. Use cons to add your name to it and store it in a new list called my-name.

Use conj to add all of your siblings to a list called me-and-my-siblings (if you don't have any or don't have that many you can use some of the Kardashians).

Print all the names in me-and-my-siblings. Then print the third name on the list me-and-my-siblings.

Create a map with all of your siblings' names as keys and their birth years as values. Use assoc to add your name and birth year to the map. Use the map to print everyone's name and their age in the year 2050.

Problem 3

Create a map with the number of days in each of the months called days-in-months. Use Clojure keywords like :jan and :feb as the keys and the number of days in the months as the values.

Create a second map from the first that has 29 days for February. Call this one days-in-months-leapyear. Make sure to do this efficiently, use assoc to create a new value for February. Finally, print each of the maps.

Functions

Next, I'll discuss creating and calling functions in Clojure. Clojure is a functional programming language so this is a pretty important topic.

The first two programs show how to write functions in Clojure.

This program shows how to use a map to encapsulate data along with some functions that manipulate the data.

These programs show how to read and write to a file using functions.

Hands-On Practice

Problem 1

Write three mathematical functions:

  • square squares a passed in parameter

  • cube cubes a passed in parameter

  • pow raises a base number to an exponent

For this group of functions, do not use any built-in mathematical functions.

Hint: look at the Clojure function repeat and reduce for the pow function. Use the let function to hold temporary values so that they can be referred to later.

Problem 2

Write a function that takes a number and will calculate the factorial value for that number.

5! is 5 * 4 * 3 * 2 * 1 = 120

10! is 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 3,628,800

Hint: this type of problem is typically done with recursion. But for now, try to do it without recursion. Look at the Clojure range and reduce functions.

Problem 3

Write a function that determines whether a number is prime or not. Use the range and filter functions to filter out non-primes in a range of values.

Hint: look at the not-any? and mod functions for determining whether a number is prime or not.

Problem 4

Write a function that takes one or more string parameters, converts them to numbers, and then adds them together and returns the sum.

(println (add-strings "10")) ;prints 10

(println (add-strings "10" "20")) ;prints 30

(println (add-strings "10" "20" "30")) ;prints 60

(println (add-strings "10" "20" "30" "40")) ;prints 100

Use Clojure's reduce function or apply to turn the strings into numbers and then add them together.

Closures

A closure is a way to associate data permanently with a function.

The first program shows how to bind data to a function.

The second program shows a more complex example of using closures.

Hands-On Practice

Problem 1

Write a function that returns a function. The Fibonacci numbers are a sequence of numbers that are generated by summing the previous two numbers together:

0 1 1 2 3 5 8 13 21 34 ...

Usually, the first two numbers are assumed to be 0 and 1. But in this case, return a function that uses the specified first two numbers in the sequence. Then generate and print out the requested numbers in the sequence.

(defn fibs-fn[firstFib secondFib] ...) ;;fill this in

(def another-fib (fibs-fn 10 15)) ;;create a fib function that prints starting with 10 and 15

(another-fib 5) ;;print first 5 fibs: 10 15 25 40 65

Recursion

A recursive function is one that calls itself. Since every function call results in a stack frame being placed on the call stack, regular recursion runs the risk of 'blowing up the call stack'. Tail recursion, with recur, is an efficient way to simulate recursion without the downsides.

Hands-On Practice

Problem 1

Write a recursive function to reverse the letters of a string.

(reverse-string "Mark Mahoney") ;;returns "yenohaM kraM"

You may need to create a recursive 'helper' function that takes a different number of parameters than the non-recursive function or use the loop form. Make sure you use recursion with the recur form so that you do not 'blow the stack'.

Problem 2

Write a function that will join a series of strings together separated by another string. The function should create and return a new string. Use recursion with recur.

(defn join [separator & parts]
   ;;your code here)

(join ", " "Mark" "Laura" "Buddy" "Patrick" "Willy") ;;"Mark, Laura, Buddy, Patrick, Willy"

Problem 3

Write a function that takes in an integer and converts it to a string. The user can specify the base that the string is in from 2-10. The program should use recursion and recur (either a recursive function or the loop form).

(to-string 100 10) ;;"100" decimal
(to-string 7 2) ;;"111" binary

Lazy Sequences

A lazy sequence defers the cost of creating values until they are needed. The first program shows how to create a lazy sequence. The next two are more concrete examples.

Hands-On Practice

Problem 1

Create a function that generates a lazy sequence of squares. For example, (1 4 9 16 ... to infinity). Use it to print the first 10 squared values.

Then write a function that generates a lazy sequence of values raised to a power:

(defn lazy-pow [start-val power]
  ...)

(take 6 (lazy-pow 10 2)) ;(100 121 144 169 196 225)
(take 6 (lazy-pow 10 3)) ;(1000 1331 1728 2197 2744 3375)

Problem 2

Write a function that generates an infinite lazy sequence of possible permutations of the passed in sequence.

(take 3 (lazy-perm ["a" "b" "c"]))
;("a" "b" "c")

(take 12 (lazy-perm ["a" "b" "c"])) 
;("a" "b" "c" "aa" "ab" "ac" "ba" "bb" "bc" "ca" "cb" "cc")

(take 39 (lazy-perm ["a" "b" "c"])) 
;("a" "b" "c" "aa" "ab" "ac" "ba" "bb" "bc" "ca" "cb" "cc" "aaa" "aab" "aac" "aba" "abb" "abc" "aca" "acb" "acc" "baa" "bab" "bac" "bba" "bbb" "bbc" "bca" "bcb" "bcc" "caa" "cab" "cac" "cba" "cbb" "cbc" "cca" "ccb" "ccc")

(take 120 (lazy-perm ["a" "b" "c"]))
;("a" "b" "c" "aa" "ab" "ac" "ba" "bb" "bc" "ca" "cb" "cc" "aaa" "aab" "aac" "aba" "abb" "abc" "aca" "acb" "acc" "baa" "bab" "bac" "bba" "bbb" "bbc" "bca" "bcb" "bcc" "caa" "cab" "cac" "cba" "cbb" "cbc" "cca" "ccb" "ccc" "aaaa" "aaab" "aaac" "aaba" "aabb" "aabc" "aaca" "aacb" "aacc" "abaa" "abab" "abac" "abba" "abbb" "abbc" "abca" "abcb" "abcc" "acaa" "acab" "acac" "acba" "acbb" "acbc" "acca" "accb" "accc" "baaa" "baab" "baac" "baba" "babb" "babc" "baca" "bacb" "bacc" "bbaa" "bbab" "bbac" "bbba" "bbbb" "bbbc" "bbca" "bbcb" "bbcc" "bcaa" "bcab" "bcac" "bcba" "bcbb" "bcbc" "bcca" "bccb" "bccc" "caaa" "caab" "caac" "caba" "cabb" "cabc" "caca" "cacb" "cacc" "cbaa" "cbab" "cbac" "cbba" "cbbb" "cbbc" "cbca" "cbcb" "cbcc" "ccaa" "ccab" "ccac" "ccba" "ccbb" "ccbc" "ccca" "cccb" "cccc")

Macros

A macro specifies some code to be executed, sort of like a function, but it also allows some of that code to be replaced with values that come from the user.

The code in a macro is kind of like a template that can be altered to suit the caller’s needs. With this powerful feature, the language can be expanded to do things that the language inventor never thought to add.

Hands-On Practice

Problem 1

Write a macro that takes in a grade earned on a student assignment (on a 0-100 scale) and some code to execute if the grade is a passing or failing.

(defmacro eval-grade [grade if-passing if-failing] ...)

And use it to print or call a function based on the value of the grade

(def users-grade 43)

(eval-grade users-grade (println "Passing") (println "Failing")) ;;"Failing"

(eval-grade users-grade (praise users-grade) (warning users-grade)) ;;call the warning function

Concurrency

Concurrency in Clojure is a big topic. I start by discussing threads. Then I talk about different strategies for dealing with data that is shared among different threads.

Hands-On Practice

Problem 1

This lab asks you to create a Clojure program that will count how many primes are in a given range.

Create a thread pool and have each thread check a single number in the range. If it finds a prime, it will increase a counter (which should be an atom since it is shared by all of the threads). Look at the program above on Atoms as a starting point.

Conclusion

If you've made it this far, you’ve already taken meaningful steps toward learning a language that can change how you write and think about code.

Clojure offers a fresh perspective on programming, one that focuses on simplicity, immutability, and the power of functions. Learning Clojure will change your brain and you will take these lessons with you to other languages as well.

So keep experimenting, keep asking questions, and keep practicing to sharpen your skills.

Comments and Feedback

You can find all of these code playbacks in the free 'book', An Animated Introduction to Clojure. There are more free books here:

Comments and feedback are welcome via email: mark@playbackpress.com.