July 20th, 2008
Functional
Recently I tried to rewrite a particular algorithm for generating permutations in a functional manner. The original was written in C#, therefore lots of mutable data structures etc. My first attempt at rewriting the code in Clojure went something like this:
As you can see, I got lost halfway through. But this is what you get when you
try to write procedural/OO code in a functional language, using immutable
data structures. Usually the right way to get out of a mess like the one above
is to use map or its syntactic brother: list comprehension (Clojure’s for macro).
I ended up with a lazy list of permutations, where the laziness required zero effort from me - got that for free from the language.
I’m still stumbling while writing programming in Clojure, but not in the frustrating way like when I tried to write something of use in F#. Probably getting to know Scheme beforehand helped a bit.
