Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hello Ninjas, I will introduce you to a practical, powerful, elegant, and fun-to-use programming language that is none other than Clojure programming language.
This article gives an introduction to Clojure programming language. This article will also tell you its usefulness and how to install and use it.
Clojure is a functional, general-purpose programming language. It is entirely dynamic; every feature Clojure supports is carried out at runtime. It is a dynamic development environment that combines a scripting language's interactive development with the infrastructure for multithread programming. Like Scala and Kotlin, Clojure programming language also runs on JVM(Java Virtual Machine). Anyone with experience with Lisp languages will find it very familiar. This is the reason why it is considered to be a Lisp derivative.
Clojure History and Rationale
Clojure is a modern, dynamic programming language that runs on the Java Virtual Machine (JVM) and was first released in 2007 by Rich Hickey. Hickey, who had previously worked on other programming languages and distributed systems, became frustrated with the limitations of existing programming languages and systems and sought to create a more powerful and flexible alternative. Hickey's main goal for Clojure was to provide a modern, dynamic programming language suitable for concurrent programming on the JVM. Integrate a variety of libraries and tools in the Java ecosystem. While maintaining their power and flexibility, they provide a simpler and more explicit syntax than other functional programming languages like Lisp.
What is Lisp?
Lisp stands for List processing. It is a group of programming languages designed to make working with strings easier. It is one of the oldest high-level programming languages still in use and has been crucial in developing many other Languages.
Installation
For Linux and macOS, installers and convenience scripts are present, making it very easy to install Clojure. For Windows, no such stable installers are available; only an early-release version is present. Until the release of a stable version, Windows users can use online interfaces for the Clojure programming language. Also, Windows users can use WSL, Cygwin, and similar things to run Linux commands on their computers. For the installation presence of Java is a prerequisite.
Installation in Linux/macOS using brew
We can use a package management system like brew to install the Clojure programming language in Linux or macOS. It is required to be pre-installed. Using brew, we can download and install the Clojure package. To install Clojure, run the command:
$ brew install clojure
This command will install the latest stable version available for Clojure. Now, after installing, to run Clojure, you need to use clj or clojure. Using this command will give access to the Clojure REPL environment.
$ clj
Installation in Linux using script installer
For Linux, an installer is available to install the Clojure programming language. It is a shell script we can use to install Clojure on Linux.
This can also be used in Windows if you use WSL or similar things that allow Linux commands.
Clojure REPL
The REPL stands for Read-Eval-Print Loop. As its name suggests, it can read Clojure code, evaluate it, print it, and run in a loop, so it always shows another prompt right after. The Clojure REPL is a programming environment allowing the users to interact with Clojure programs at runtime and modify them by calculating the value of one code at a time. It is the Clojure equivalent of the JShell tool for Java 9 and above. This allows the user to instantly enter the Clojure code and see its output.
When the REPL environment is loaded, its prompt indicates the current namespace where we are working now.
Basic Syntax
The syntax of the Clojure programming language is very similar to that of Lisp languages. A lot of codes that we write in Clojure are expressed in the form of Lists. When evaluated, these produce results either in the form of Lists or simple values.
For example:
(+ 1 2 3 4 5)
You can also try this code with Online Java Compiler
This will add up all the numbers after the ‘+’ operator. This list can be of any length; it will do the same operation will all the elements of the list, starting from the second element.
Printing Hello World
The method to print Hello World in Clojure programming language is very similar to that in Java. We can use the printline function for this. The command is:
(println “Hello World”)
You can also try this code with Online Java Compiler
It gives two outputs one is Hello World, and the other is nil(equivalent to null in other programming languages) which means no value.
Comments in Clojure
For comments, ‘; ’ is used in Clojure. Any number of characters after ‘; ’ is considered as comments and is not compiled. For example:
Data-types
The data types available in Clojure are similar to those in Java. The reason is that Clojure programming language is built on JVM(Java Virtual Machine). In Clojure, we do not need to declare the data types explicitly; that is done automatically.
Some of the basic and most widely used data types are:
We saw above how to store different types of data. But when we have to store more than one data for that condition, Clojure possesses a full set of collections. These are:
Vectors
Lists
Sets
Maps
Vectors
In Clojure programming language, an ordered list of values is called a vector. It can hold any arbitrary value inside it; these can even be different collections. It is an indexed collection.
Lists
In Clojure programming language, a list is an ordered collection of values. It is implemented as a Linked list. This is the main difference between vectors and lists in Clojure. Vectors are more sort of Array lists, whereas lists are like Linked lists.
Sets
In Clojure programming language, a set is defined as an unordered collection of values. The values stored in the set need to be unique; it does not contain the same value more than once. For example:
Maps
Just like other programming languages, in Clojure also, the map is a set of key-value pairs. It is also an unordered collection. Just like sets, the order of output depends on how they are stored in memory.
Defining Functions
Functions in Clojure are first-class citizens, which means they can be assigned to variables, passed as arguments to other functions, and returned as results from functions. This makes it easy to write modular, composable, and reusable code.
Functions are defined using the defn macro.
Example:
(defn add [a b] (+ a b))
Local Variables
In Clojure, you can define local variables within a function using the ‘let’ special form. let allows you to bind values to symbols and use them within an expression.
Example:
(defn multiply [a b]
(let [result (* a b)]
result))
One important thing to note about let is that it is a special form, not a function. This means that the syntax is slightly different from function calls. In particular, the bindings in the vector are not evaluated as function arguments but are evaluated in order from left to right. Also, the entire let expression returns the value of the last expression in its body, which is the value of the result in this case.
Local Functions
In Clojure, a local function is defined within another function's scope. Local functions are helpful in breaking down complex tasks into smaller, more manageable functions and encapsulating functionality only used within a single function.
Example:
(defn my-fn [x y]
(let [z (+ x y)]
(fn [a b]
(+ a b z))))
The above example defines a function my-fn that takes two arguments, xand y. Inside my-fn, we define a local function using fn that takes two arguments, a and b. The local function adds a, b, and z (the sum of x and y) and returns the result.
Structs
In Clojure, structs are a way to define data structures with named fields. Structs are similar to maps but have a predefined set of fields and additional features such as inheritance, default values, and validation.
Structs are defined using the defstruct macro, which takes a symbol representing the name of the struct and a list of field names.
Example:
(defstruct person :name :age :gender)
In the above example, we define a struct named person with three fields: name, age, and gender.
Frequently Asked Questions
How is Clojure related to languages like Kotlin and Scala?
The Clojure programming language is related to languages like Scala and Kotlin because all these are derived from Java and run on the Java Virtual Machine.
Is Clojure faster than Python?
Clojure, being a compiled language, can be faster than Python, which is an interpreted language. However, performance can vary depending on the specific use case, implementation details, and optimization efforts.
Is Clojure a good programming language?
Clojure is a powerful and expressive programming language that is well-suited for functional programming and concurrent programming. It has a strong community and is widely used in industry for a variety of applications.
Name the ordered and unordered collections in Clojure programming language.
The ordered collections are Lists and Vectors. The unordered collections are Maps and Sets.
How is the order decided for the unordered collection?
The order in which we see the output of the unordered collection depends on the way they are represented in the memory.
Conclusion
In this article, we learned about a new programming language, Clojure. It is different and very fun to use. It is similar to that of Lisp languages. We started with learning how to install it, then learned its basic syntax and Data types. We also read about the widely used collections it has.
We hope that this blog was helpful to you and gave you basic knowledge of Clojure programming language. To learn more about JVM and JDK, visit JVM and Its architecture and Java Development Kit(JDK), respectively.