November 24, 2014

A Clojure adventure into Project Euler

After writing some C# a couple of weeks ago, I realised that I have gotten very rusty as a coder, resulting in code that was unwieldly, over-engineered or plain bizarre. This made me rethink my priorities, and eventually I decided to go back a number of steps and work on the fundamentals once again.

The first thing that came to my mind was the Code Wars website (brilliant website), only problem with it was that it focused on four languages at the time, Python, Ruby, Coffee Script and JavaScript. I have some experience working with Python and Ruby and some minor experience with Java Script so I should have been fine with either of the them. So while I was sitting here pondering whether I should go for Python or Ruby, it struck me that maybe, perhaps I would fall into the same problem as I had with C#? I have a natural tendency to over-engineer stuff I work on at times, resulting in a bizarre unholy mix of pristine clock-work and duct tape among my personal projects and systems. So, is there an alternative? Turns out there is, at least for me. I thought back on when I was taking a course in Haskell, I could both have the cookie and eat it too.

Let me explain; I have an keen eye for detail, and I like things to be “just so”. This leads to generalisation which can quickly drown you. A simple 50 line program ends up being 50 lines + 500 lines boiler plate. It might look pretty, but it is not particularly fun to mess around with. Compare it with this:

bmiTell :: (RealFloat a) => a -> a -> String
bmiTell weight height
  | bmi <= skinny = "You're underweight, you emo, you!"
  | bmi <= normal = "You're supposedly normal. Pffft, I bet you're ugly!"
  | bmi <= fat    = "You're fat! Lose some weight, fatty!"
  | otherwise     = "You're a whale, congratulations!"
  where bmi = (weight / height^2)
        skinny = 18.5
        normal = 25.0
        fat    = 30.0

(source: http://learnyouahaskell.com/syntax-in-functions#guards-guards)

Harsh comments aside, this code is very pretty to me and that was generally the case with most Haskell I saw during my course (~ Haskell 101). Which made me ponder the Python/Ruby question a bit harder. Until, suddenly! And after getting about 20% into the article I issued yum install emacs

Long story short, I eventually settled on Clojure. The functional style is pretty to me, and at the same time not what I am used to, “win-win” in other words. It has now been a couple of weeks with little coding but quite a lot of thinking and pondering. I am getting back to being able to solve the problem at hand without getting too caught up in “design”. After getting through a couple of tutorials and verified by Project Euler that I am able to solve problems in Clojure I have decided to try and complete as many as I can.

Posting these is partly for my own benefit though, hopefully, they might be of interest to someone else as well.

© Sebastian Hörberg 2018

Powered by Hugo & Kiss.