4Clojure banner

Coding Challenges: 4Clojure

4Clojure.com is an excellent website to help you discover and apply the common functions in the Clojure language.

The website is self-contained so you do not need to install anything, simply paste in the missing code from the tests. One piece of code should solve all the tests for that challenge.

Press the Run button to see if the code satisfies the tests, turning green if successful and red if they fail.

The Problem List shows the challenges categorized by experience level required, (Elementary, Easy, Medium, Hard) to solve them. Start with the easiest problem or work your way through the challenges in any order you wish.

There are over 550 functions in the clojure.core namespace alone, with additional functions in many other namespaces that make up the Clojure Standard Library. It is not required to learn all these functions to be productive in Clojure.

Before you start

Create a free account to be able to track your progress through the challenges.

Login to your account and visit the Top 100 users. Select the Following tick-box next to a number of the top users. Once a challenge is solved you will be able to see the solutions of those users you follow.

Alternatively, visit a specific user profile, e.g. practicalli, and click follow so you can see their solutions.

Clojure code challenges - 4clojure.com top users

Get help

Look at the Clojure Cheatsheet and Clojure API for an understanding of what functions are available in the core of the Clojure language.

Search directly in ClojureDocs for functions. Each function has a page that describes the function, shows the arguments it takes and provides many examples of its use. At the end of the page are related functions too.

An Internet search of clojure topic, where topic is a name of the thing you want to do, should return many examples of functions that could be useful to solving the challenge. Or

Using let and anonymous functions

The solution submitted should be a single form, which is inserted in the test code where the __ underscore placeholder is. It is therefore not possible to define data with def or a separate function with defn to support the submitted solution.

Use the anonymous function, fn, to define behavior.

(fn [value1 value2]
  (* value1 value2))

Use let to bind a name to a value, so that value can be re-used throughout the expression. let is also useful for breaking the algorithm into smaller pieces, making it easier to solve the challenge.

(let [name value]
  (* 2 value (/ value 4) (+ value 3)))

It is common to combine fn and let to solve the challenges as they grow in complexity

(fn fibbonacci [length-of-series]
  (let [fib [1 1]]
    (if (< (count fib) length-of-series)
      "iterate... to implement"
      fib)))

My function is not working

4Clojure uses Clojure 1.5 version and whilst the core of the Clojure language has not altered greatly since 4Clojure was created, there may be a few newer functions that are not supported.

Try the code in a Clojure REPL or create a Clojure project using the latest version of Clojure (1.10.1).

References

results matching ""

    No results matching ""