DEV Community

Cover image for Programming in Clojure (Part 1 Basics)
Mark Mahoney
Mark Mahoney

Posted on • Updated on

Programming in Clojure (Part 1 Basics)

This 7 part series of posts will cover programming in Clojure. Clojure is an awesome functional programming language that runs on Java's Virtual Machine (JVM). It is sometimes called a LISP or LISt Processer. Through a series of code 'playbacks' I will guide you through the basics of the language.

I am expecting that the reader already has some programming experience in another language but Clojure is so different than 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 beginners guide to programming you can find some of my other programming content here.

I will cover basic flow, data structures, immutability, functions, closures, tail recursion, lazy sequences, macros, and concurrency. Clojure is a functional programming language, 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 want to take advantage of modern hardware on laptops and mobile phones (multiple processors sharing a single memory).

Simply watching an experienced artist paint is not enough to say that you have learned how to become a painter. Watching an experienced artist is an important part of the learning process but you can only call yourself a painter after struggling to make your own paintings first. There are lot of similarities between learning to paint and learning to program. The only way to truly learn to program is to practice! Clojure runs on the JVM and it can be difficult to set up a programming environment. So, I recommend using 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.

So, let's get started. You will be asked to follow along with the programs below. Just click on the links and a code playback will load (you might want to open each one in a new tab). Then click on the comments on the left hand side of the screen or hit the play button to drive the development of the code. You can download the code at any point or copy/paste it into a repl on replit. There are some controls in the top right hand side of the screen to make the text bigger or to switch to blog mode (which is good for small screens).

Introduction to Clojure

These first few programs show how to print to the screen, perform basic arithmetic, and store some data.

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

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

Call to Action

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 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 you can use some of mine or make some up).

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

Create a map with all of your siblings' names as keys and their ages as values. Use assoc to add my name/age to the map (my age is 48). Use the map to print your age.

Problem 3
Create a map with the number of days in each of the months called days-in-months. Use the integers 1-12 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). Create another map with month names as the strings.

Prompt the user for a month number, day number, and year and create two new variables, short-string and long-string. Short string will be in this format month/day/year and long string will be in this format "MonthName dayNumber, Year". Print out both of the strings and the number of days in the month you were born.

Comments and Feedback

You can find all of these code playbacks in my free 'book', An Animated Introduction to Clojure. I am always looking for feedback so please feel free to comment here or to send me a message. You can follow me on twitter @markm208.

Top comments (1)

mattmoranjava profile image
Matt Moran

The source code links are broken - has taken them down?