DEV Community

Jidneya
Jidneya

Posted on • Updated on

Learning Clojure Part 1

After being persisted by my friends to learn Clojure, I finally caved and now we are here. I am not an expert going into this i am in fact as clueless as a beginner with experience mainly in java and python. SO please feel free to correct any mistakes i may have made

What is Clojure

For those people who don't know, Clojure is a functional programming language that runs on JVM (java virtual machine). It is described as a dynamic general purpose language, and every feature is supported at runtime. What this means is that you can run commands at run time and we will get to know this better over the course of the series.

How to get Clojure running

There are many ways to do this for different operating systems and i suggest you go look at some videos, or articles suited to your operating system to get Clojure running. Usually you would need:

  • Leiningen, which is a build system for Clojure
  • A JDK, as Clojure runs on JVM
  • A IDE, in my case i use IntelliJ, not for any particular reason, simply because I do all my java in it

Actually writing some code

Now to the fun bit, coding. It would be worth noting that i am following tutorialspoint.com for learning, so you can always check them out if you ever don't understand anything. Also we write Clojure in a .clj file.

Hello World

What better way to start than writing a hello world prompt:

(ns clojure.examples.hello
(:gen-class))
(defn HelloWorld []
(println "Hello World"))
(HelloWorld)

This is one way to do it, and I think that will explain the basics well.
'defn' is how we define a function, and in functional programming, functions play a pretty big role, but we will come onto that later.
Our 'HelloWorld' function is defined to print the statement "Hello World". To call the function we simply put call it in a pair of parenthesis.

Of course we could simply do something like
(ns clojure.examples.Main
(:gen-class))
(println "hello world")

But i feel like this is not giving the right feel for Clojure from what I have seen, as it's true capabilities lie in it's functional side.

Prefix notation

There is just one more thing that I would like to touch upon in this article and that is prefix notation. This simply means that operators are placed before operands, two examples below:
(+ 1 2)
(str "Hello" "World")

In the first line we are simply doing: 1 + 2, but in prefix notation we put the '+' first . You can think of the '+' being the function and the 1 and 2 being it's 1st and 2nd parameters respectively.

It is the same for the second line. We are simply using the str operator to concatenate (add strings to make one big string) the two strings given , which can also be considered as parameter

Conclusion

Well that is it for today. I hope that my explanation of the basics was good enough peak your interest and I hope that you will join me in this journey. Please feel free to help me through the comments if you are already experienced at this. Thank you and until next time.

Top comments (0)