99 Clojure Problems*




Most programmers approaching Clojure already have programming experience. They mostly lack functional (and sometimes Java) programming experience. It is helpful to have a list of problems to solve while learning Clojure. The purpose of this list of Clojure problems is to give the student of Clojure a nice variety of problems to solve while learning Clojure.

This list borrows heavily from a Prolog problem list by werner.hett@hti.bfh.ch. As with the original list, each problem is tagged as easy (*), intermediate (**), and difficult (***). To see solution(s) for each problem, click the problem number.

Also, expect to see problems and solutions from Project Euler. The idea is to provide fun challenging problems with which to practice learning Clojure. Project Euler fits the bill on that one.

This list of problems is not intended to be solved in order. Feel free to skip around a bit, or stick to problems of the difficulty level that interests you.

Some of the "easy" problems can be solved quickly with built in Clojure functions. After solving the easy problems using built in capabilities of Clojure, I encourage you to solve the problem a second time without the built in Clojure solution. This will give you greater depth of understanding of Clojure.

Send comments, suggestions for interesting problems, alternate solutions, and corrections to:



Tenative List of Problem Categories



Clojure Basics

POO1 (*) Find the last element of a list.

      Example:
      (get-my-last '(a b c d))
      d

POO2 (*) Find the last two elements of a list.

      Example:
      (get-last-two '(a b c d))
      (c d)

POO3 (*) Find the nth element of a list using a zero based index.

      Example:
      (value-at, '(a b c d), 2)
      c

POO4 (*) Find the number of elements in a list.

POO5 (*) Given a list, create a new list with the same values in reversed order.

POO6 (*) Determine if a list is a palindrome.

POO7 (**) Flatten a list with nested lists.

      Example:
      (flatten-list '(a (b (c d) e)))
      (a b c d e)

POO8 (*) Given one side, find the perimeter of a square.

POO9 (*) Given two sides of a right triangle, calculate the hypotenuse of the triangle.

PO10 (*) Given the height and radius of a cylinder, find the volume of the cylinder.

PO11 (*) Create a hashmap of birthdates associated with string names as keys. Request a birth date from the hash map by passing a name to a function. The function should return the correct birth date, or a human readable message stating the person's birthdate is unknown.

PO12 (**) Given a typical Tower of Hanoi problem with three towers and six rings on one end, print the moves necessary to move the rings to the other side.

PO13 (**) Eliminate consecutive repeated elements in a list. Duplicates may remain in the list, but not next to each other.

      Example:
      (remove-duplicates '(a a a a b c c a a d e e e e))
      (a b c a d e)



Java Interop

P1O1 (*) Pop up a JOptionPane Message Dialog. End the program after the dialog is closed by the user.

P1O2 (*) Display a JFrame with a title, and JLabel. The JFrame should be 600 pixels wide by 400 pixels high.

P1O3 (*) Display a JFileChooser. Choose a directory. Print the absolute path to the chosen directory.

P104 (**) Given a loan date in mm-dd-yyyy format, calculate the days from the loan date until the present day.




Advanced Clojure

P2O1 (*) Create a function to divide two numbers. Divide by zero. Catch the exception, and explain that division by zero is prohibited.

      Example:
      (my-div 3 0)
      Division by zero is prohibited.



























*I say "99" because the inspiration for this page had 99 problems.