musings of a Software Developer

Seven languages in seven weeks day : My solutions for clojure day 3


Day 3 of clojure was al about concurrency. It introduces the software transactional memory, references, agents, futures and atoms.

The first exercise

The first exercise is to create a vector of accounts in memory, and create a debit and credit function to change the balance of an account within the vector.

I just implemented it in a very basic way, using a reference and accessing it with the dosync function.

Nothing else to say about it, except it is really a small about off code you need to have a program supporting concurrency.

Exercise 2

The next exercise was a lot more challenging. It is the sleeping barber problem as described on this link.

Here is my solution.

So I basically used two atoms to represent the state that is to be manipulated by concurrent threads. And in the functions I use swap to alter the data with the atoms.


All with all I really grew to like clojure. The first few times someone showed it to me, I really didn’t understand a thing, not being used to the lisp like syntax, but when you get accustomed to it, it gets a lot easier to read, and in the end I really found the code quite beautiful.
If I compare it to scala, I find clojure a neater, more powerful language(based on this 3 chapters, so basically nothing ;-)) and would rather invest some time in clojure.

Ok next chapter will be haskell.

Leave a Reply

Required fields are marked *.