(defn hello-world  (println "Hello world!"))
This defines a function (using the
defn macro) called
hello-world that takes no input (
) and prints
"Hello world!" to the console. We can call it like so:
(hello-world) ; => Hello world! ; nil
All variables in Clojure are immutable, and can’t be changed using an assignment operator (
= in most C-like languages) or by a function. All functions aim to be referentially transparent, which means that if you give them the same input, they should give the same output no matter what. The
hello-world example above is referentially transparent - it will always print “Hello world!” no matter what. Something that relies on a random number generator is not referentially transparent, because its output is random.
(defn random-function  (if (> (rand 4) 2) ; if a random number between 0 and 2 is greater than 2... (println "foo") ; ...print "foo". otherwise... (println "bar"))) ; ...print "bar"
While not being able to change variables might sound nightmarish, it’s a lot easier than you think, especially if the language is based around it (like Clojure is!), and avoiding unnecessary mutation can make your code much less buggy.
(.indexOf [1 2 3 4] 2) ;; .indexOf is a Java method! ; => 1
“Concurrency” here means “one program working on multiple threads at once,” which can make your code much faster. It can also make your code much buggier; imagine if two different functions were changing and reading from the same object at once! It would be utter chaos. Thankfully, in Clojure, variables are immutable, which means there’s no chance of this kind of mayhem breaking loose. The language also has a variety of features to make concurrent code easier, such as the Software Transactional Memory system, agents and atoms.
|Table of Contents||Basics|