Sebastien Lachance

Functional programming : Starting to learn Clojure

 

The time has come for me to add another language into my toolbox and I decided to look at a functional language : Clojure. I have the Programming Clojure book, so why not?

Clojure is a modern Lisp language.

Yes, Lisp (LISt Processing). I never thought I would have been near or around it. I heard so many bad things about his complexity and how it would not useful in real life scenarios. But I have heard sometime about programmers who expended their mind to new programming concept with it. The following comment seems to be universal from programmers who knows it well.


I struggled to learn it, but once I finally get a grasp on it, it made me a better developer.

Why?

In our imperative language (Java, C#, Ruby, etc.) there is really no support for multi-core processing. That is, your program will only run on one core. Clojure is designed to support multi-core in mind and had support for dealing with concurrency really well.

What is Lisp?


From Wikipedia :

Lisp was originally created as a practical mathematical notation for computer programs, influenced by the notation of Alonzo Church’s lambda calculus. It quickly became the favoured programming language for artificial intelligence (AI) research. As one of the earliest programming languages, Lisp pioneered many ideas in computer science, including tree data structures, automatic storage management, dynamic typing, and the self-hosting  compiler.

Just that. And I would like to add that it had a lot of parentheses. A lot.

Clojure is emitting JVM bytecode and goes hand in hand with Java but maybe not.


I was afraid of that. I know Java, but not the whole library. Since Clojure can and will use the library, you better know where you are heading. But again, if you want to learn fast, just jump in the shark pool. But, Java is absolutely not required to program with it if you don’t want to.

Clojure Code


Let’s look at an example :

(loop [result [] x 5]

(</span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (zero</span><span style="color: #000000;">?</span><span style="color: #000000;"> x)
    result
    (recur (conj result x) (dec x))))</span></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>


Output : [5 4 3 2 1]

This snippet of code is actually a loop from 5 to 1 and appending each time the current index to an array

This is not a tutorial but let’s dig deeper at (zero? x). In Clojure, a function that evaluate a condition have the name of it appended of an ?. So zero? is the function name and x is the argument. Yes, you read right, you don’t supply function’s argument like other programming language where it would look like :

bool isZero = IsZero(x);

Conclusion

I tried to present a little introduction to Clojure without going into too much details. I am not yet comfortable with every concept and what would be the advantages in real life. But, It will be an interesting trip. After a little more than three weeks now, and almost 10 hours of playing around and I can already say that I will need to see things differently.Very differently.

 

Technorati Tags: ,

Comments