পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Friday, January 27, 2017

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

No comments:

Post a Comment