Seven Languages in Seven Weeks: Clojure Day 1

Up until now, I have programmed the solutions for the Seven Seven Languages in Seven Weeks questions using a simple text editor, Scribes, and running them in the console.

If you use Linux and a WM that runs GTK, I suggest giving Scribes a spin. It has a minimalist interface that gets out of your way and offers many keyboard shortcuts (press Ctr-H for help on shortcuts). If you run on Ubuntu, consider installing it from a PPA package, as the Ubuntu version (at the time of this writing, in 10.04 LTS) is, to quote Scribes website:

It’s old, buggy and unsupported!

The reason I had been using a simple set up was to avoid having to learn new IDEs and the like for each language and concentrate simply on the code. Plus there is something to be said for such a setup when you just want to quickly get coding at home.

With Clojure, you have to do some project setup. For this, the book suggests using Leiningen which is a powerful and popular tool for working with Clojure projects.

Rather than use Leiningen to start my project and then use Scribes afterwards, I found it was easier to use an IDE, Eclipse, and use a Clojure plugin, CounterClockWise. Your mileage may vary, but if you are starting the Clojure chapter, I recommend trying CounterClockWise.

Here were the questions:

Write a function that returns whether a string is longer than n characters.

(ns day1q1)

(defn big [st n] (> (count st) 4))

(big "This is a test" 4)

Write a function that returns the correct collection type from amongst the following :list, :map:, :vector.

(ns day1q2)

(defn collection-type [col] 
     (list? col) :list
     (map? col) :map
     (vector? col) :vector))

(collection-type [:hutt :wookie :ewok])
(collection-type (list 1 2 3))
(collection-type {:chewie :wookie :lea :human})

As for Clojure itself, it’s a dialect of Lisp that runs on the JVM. If you did not know, programming in Lisp gives you instant geek cred.

Jokes aside, I was inspired to learn new languages, and to read Seven Seven Languages in Seven Weeks by a famous article entitled the Beating the Averages, which is mostly about Lisp. If you don’t want to read the whole thing, consider at least reading through The Blub Paradox part. This part is very thought provoking.

Along with What Is Software Design?, Beating the Averages (for it’s Blub Paradox) are the two articles/blog entries that I would recommend every developer should read.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s