Clojure - Variables



In Clojure, variables are defined by the ‘def’ keyword. It’s a bit different wherein the concept of variables has more to do with binding. In Clojure, a value is bound to a variable. One key thing to note in Clojure is that variables are immutable, which means that in order for the value of the variable to change, it needs to be destroyed and recreated again.

Following are the basic types of variables in Clojure.

  • short − This is used to represent a short number. For example, 10.

  • int − This is used to represent whole numbers. For example, 1234.

  • long − This is used to represent a long number. For example, 10000090.

  • float − This is used to represent 32-bit floating point numbers. For example, 12.34.

  • char − This defines a single character literal. For example, ‘/a’.

  • Boolean − This represents a Boolean value, which can either be true or false.

  • String − These are text literals which are represented in the form of chain of characters. For example, “Hello World”.

Variable Declarations

Following is the general syntax of defining a variable.

Syntax

(def var-name var-value)

Where ‘var-name’ is the name of the variable and ‘var-value’ is the value bound to the variable.

Example

Following is an example of variable declaration.

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   ;; The below code declares a integer variable
   (def x 1)
   
   ;; The below code declares a float variable
   (def y 1.25)

   ;; The below code declares a string variable
   (def str1 "Hello")
   
   ;; The below code declares a boolean variable
   (def status true))
(Example)

Naming Variables

The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because Clojure, just like Java is a case-sensitive programming language.

Example

Following are some examples of variable naming in Clojure.

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   ;; The below code declares a Boolean variable with the name of status
   (def status true)
   
   ;; The below code declares a Boolean variable with the name of STATUS
   (def STATUS false)
   
   ;; The below code declares a variable with an underscore character.
   (def _num1 2))
(Example)

Note − In the above statements, because of the case sensitivity, status and STATUS are two different variable defines in Clojure.

The above example shows how to define a variable with an underscore character.

Printing variables

Since Clojure uses the JVM environment, you can also use the ‘println’ function. The following example shows how this can be achieved.

Example

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   ;; The below code declares a integer variable
   (def x 1)
   
   ;; The below code declares a float variable
   (def y 1.25)
   
   ;; The below code declares a string variable
   (def str1 "Hello")
   (println x)
   (println y)
   (println str1))
(Example)

Output

The above program produces the following output.

1
1.25
Hello
Advertisements