Learning Clojure: Part 2, REPLs and data types
Jidneya
Posted on February 18, 2024
Hello everyone, today we will learn about the REPL in Clojure and how to use it, along with the data types supported by Clojure.
REPL
A REPL is a read-eval-print-loop. In simple terms, it is a prompt in which you type your code, it evaluates it, prints the result and then shows the prompt again. It achieves this using a running program and is useful for us to test code.
Starting a REPL session
I assume that you have already set up Clojure on your system, and below I will show how to open a REPL using Leiningen in command line, because that is what i am using. You can also do this on other IDEs like eclipse, but that is something that i am not really aware of.
Anyways we must first open up administrator command prompt and type in the following prompt
lein repl
I have attached an image below of my command prompt, and what it should look like
Special variables in a REPL session
we have three special variables in out REPL session which are '*1', '*2' and '*3', and these refer to the results of the most recent three expressions. I have given a basic prompts to the system to demonstrate these special variable below:
That is basically all we have to know for now about the REPL, now lets move onto Data types
Data types
If you have any experience in Java, you will be delighted to know that since Clojure requires Java, the data types are near identical with a few extras:
Integers
- short, long and int - these are used for whole numbers
- Octal number - used for octal representation of numbers
- Radix number - used for radix representation of numbers
- hexadecimal number - used for hexadecimal representation of numbers
Floating point - use for floating point numbers and scientific notation
char - holds a single character
Boolean - true or false
String - holds a piece of text
Nil - This is a null value in Clojure
Atom - they provide a way to manage shared, synchronous, independent state
Max and min values
Since the Clojure data types stem form the java ones, they have the same maximum and minimum values and i will list two here:
int - -2,147,483,648 to 2,147,483,647
float - 1.40129846432481707e-45 to 3.40282346638528860e+38
Now lets show this in some code. Below i some simple code with comments on the data types:
The output to the program is:
(If you were wondering i am using cursive on IntelliJ for this code)
Thank you for reading this week's tutorial. I hope it was fruitful, and i will be back soon with more content on Clojure so that we can get to better understand how to use the language.
Posted on February 18, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.