@freeCodeCamp: Clojure is a functional programming language that can change how you think about writing code. In this interactive guid…
Summary
An interactive guide introducing Clojure programming with animated code playbacks and hands-on exercises, covering functions, immutable data, recursion, and more.
View Cached Full Text
Cached at: 07/29/26, 11:56 AM
Clojure is a functional programming language that can change how you think about writing code.
In this interactive guide, @PlaybackPress teaches you Clojure through animated code playbacks and hands-on exercises.
You’ll learn about functions, immutable data structures, recursion, lazy sequences, and more.
https://freecodecamp.org/news/learn-clojure-programming-basics/…
An Animated Introduction to Clojure – Learn Clojure Programming Basics
Source: https://www.freecodecamp.org/news/learn-clojure-programming-basics/
This tutorial introduces the programming language, Clojure.Clojureis an awesome functional programming language that runs on Java’s Virtual Machine (JVM). It is sometimes called a LISP, which is short for ‘LIStProcessing’.
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 theJVM. 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 usingreplit. I recommend using it to write your first programs in Clojure. If you feel up to it, check outthese docsto 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 interactivecode playbacksthat 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 Pressis 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 builtStoryteller, 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 thebookto your bookshelf.
Table of Contents
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 usedefto store a reference to it calledempty\-list. Useconsto add your name to it and store it in a new list calledmy\-name.
Useconjto add all of your siblings to a list calledme\-and\-my\-siblings(if you don’t have any or don’t have that many you can use some of theKardashians).
Print all the names inme\-and\-my\-siblings. Then print the third name on the listme\-and\-my\-siblings.
Create a map with all of your siblings’ names as keys and their birth years as values. Useassocto 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 calleddays\-in\-months. Use Clojure keywords like:janand:febas 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 onedays\-in\-months\-leapyear. Make sure to do this efficiently, useassocto 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:
squaresquares a passed in parametercubecubes a passed in parameterpowraises a base number to an exponent
For this group of functions, do not use any built-in mathematical functions.
Hint: look at the Clojure functionrepeatandreducefor thepowfunction. Use theletfunction 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 Clojurerangeandreducefunctions.
Problem 3
Write a function that determines whether a number is prime or not. Use therangeandfilterfunctions to filter out non-primes in a range of values.
Hint: look at thenot\-any?andmodfunctions 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’sreducefunction orapplyto 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, withrecur, is an efficient way to simulate recursion without the downsides.
- Tail Recursion
- Recursion and Fizz-Buzz
- Recursion and Square Roots
- Converting a String to an Integer in the Bases 2-16
- Mortgage Scheduler
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 theloopform. Make sure you use recursion with therecurform 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 withrecur.
(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 andrecur(either a recursive function or theloopform).
(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.
- Threads
- Threaded poker
- refs and threads
- Atoms
- Poker with atoms
- Thread logging with agents
- Simpler concurrency
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 anatomsince 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.
You can find all of these code playbacks in the free ‘book’,An Animated Introduction to Clojure. There are more free books here:
- An Animated Introduction to Programming in C++
- An Animated Introduction to Programming with Python
- Database Design and SQL for Beginners
- Worked SQL Examples
- Programming with SQLite
- An Introduction to Web Development from Back to Front
- An Animated Introduction to Clojure
- An Animated Introduction to Elixir
- A Brief Introduction to Ruby
- Mobile App Development with Dart and Flutter
- OO Design Patterns with Java
- How I Built It: Word Zearch
Comments and feedback are welcome via email:[email protected].
If you’d like to support my work and help keep Playback Press free for all, consider donating usingGitHub Sponsors. I use all of the donations for hosting costs. Your support helps me continue creating educational content like this. Thank you!
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers.Get started
Similar Articles
Clojure is almost as fast as C (with some help)
This article details how Clojure, with the JVM's Vector API and careful optimization, achieved frame rates within 20% of C for a 3D stress test, demonstrating that a dynamic language can approach low-level performance on hot loops.
My thoughts after using Clojure for about a month
The author shares their experience learning Clojure over a month, comparing it favorably to Common Lisp and Scheme, praising its cohesion and pragmatic design.
Clojure Hosted on Go
Glojure is an open-source Go-hosted interpreter for the Clojure language, providing seamless access to Go libraries and allowing embedding within Go applications. It is in early development but used in hobby projects.
Mixing Visual and Textual Code
This paper presents Hybrid ClojureScript, a programming language that allows developers to mix visual and textual syntax for domain-specific expressions, preserving composability and static reasoning.
Clojure: Transducers
Official Clojure documentation explains transducers—composable, stateful transformation functions that decouple sequence processing from specific collection types.