An FP/Clojure Ah Hah! Moment

I have purchased either in e-book or printed form most of the major books on Clojure, including both from Manning Publications, Apress, O’Reilly and The Pragmatic Bookshelf . All these books are well written; all have slightly different approaches and styles.

Some are tutorial in nature, others are more like a tutorial/reference, and others contain philosophy in addition to tutorial. In some of these references, there is a sense you have programmed in Java previously, and I sense, and it is not directly implied, that the reader has dabbled in a Lisp dialect.

My Ah Hah! moment came recently when I realized that until a couple of years ago, the last languages I used in the commercial world were C++, Java 2, and Perl. Functional Programming (FP) was an academic subject, and though I had heard of Python, there was no pressing reason for me to learn it, at least back then. These books are written by authors with mostly a different programming learning and implementation environments.

So, as I work my way through Clojure In Action and Code Lesson’s Intro to Clojure, there are additional things for me to learn, the biggest of these the differing implementation environments and curriculum from when I started programming.

Here we go, then.


How We Learned Programming

The first time I touched a computer was in college. As we were graduating high school, the prevailing wisdom said don’t bother learning anything about computers. Pretty soon, they will all be programming themselves. I guess pretty soon covered a longer time span than the expected normal time span for that phrase.

My first experience was on an IBM connected teletype directly connected from the Chemistry Department in Parsons Lab at UNH to the IBM 360 computer in the Math Department in Kingsbury Hall. It was awful. You had to type perfectly, or count the number of characters as you erased backwards. A few years from that day, I used punch cards for a Fortran IV course at Northeastern University, and to me they were considerably better than a teletype. Despite enduring all this, I was spared those punched programs on paper tape.

Finally, I realized I had to have a common day higher level programming language, discrete [data] structures, and a course in someone’s assembly language — I chose IBM 370 assembler — under my belt. That allowed me a passage into software technical writing about languages and operating systems.

Those courses lead to Harvard University Extension School for something called a CAS — they have a masters program now — which included the data structures, Pascal, discrete mathematics, and computability theory, along with fleshing out operating system theory and a wonderful course called the anatomy and physiology of a computer. Before I graduated, I had already been programming for two years using PL/I.

Now, the big push is functional programming, along with a group of computer programming languages that are not so relatively new, and some, like Scala and Clojure, that are relatively new. Given Computer Science degrees were not around when I went to college it is hard to say what the curriculum changes are, but looking at typical curriculum makes me think obvious things have changed, like no longer requiring assembly language, and the math requirements have migrated to include support for functional programming.

I never heard of Lambda calculus, but having looked at the structure of more modern computer programming languages, my guess is Lambda calculus is taught a lot, if not as a separate course, then embedded in other courses. I believe I have to learn it, and I’ve started by ordering an introduction to functional programming .

Is Learning Functional Programming a Good Thing?

Those folks who follow this blog or might come to the conclusion that I’ve abandoned Python, but that is not the case. I started learning Python in 2009 by writing some applets to perform database housekeeping, and then eventually implemented an application in 2010 for our water department. This application, which includes a Django-powered web site was written almost exclusively in Python.

I have been interested in functional programming for a few years now, and started looking at Haskell a couple of years ago. As the deadline to start designing our water department application loomed closer,  I did not know Haskell well enough to use it. Also, as a language, Haskell did not appear to have the broad library support enjoyed by Python. So, I chose Python as the implementation language for this project.

Earlier this year I came across a description of The Joy of Clojure at my favorite book sellers — — and started learning more about Clojure. Having never programmed in a functional programming language or a Lisp dialect, I am finding Clojure a little daunting. However, a project has presented itself, which involves a faster way of processing our billing data through address verification.

A project is like a carrot and stick; that is it provides cause to use extraordinary effort to learn something new quickly or solve a particularly difficult problem. The project will require more than a Clojure program. Some 4GL and bash scripts will be used. However, the Clojure program will be substantial enough to get me familiar with the language and will have required enough effort to start writing code.

One of the things that has attracted me to functional programming (FP) languages is the same thing that attracted me to object oriented languages when I first learned C++ in the 1990s. Learning C++ improved my overall programming skills, whether I was writing object-oriented software or not. With learning C++, my analogy was it was like rotating your mind 90 degrees. With Clojure it is that and more.

Just one of the ways that learning Clojure has made me think differently is using the various sequence functions. Most of what I want to do with billing data fits well into Clojure’s strengths. That is you can apply the same functions to all the data and sort out data easily that does not fit your needs. At the moment, I do not see a use for using Clojure in a multi-process/multi-threaded environment.

I am also appreciating providing for data at the beginning, binding in Clojure, declaring variables in imperative languages like C. Python’s strength in the the ability to declare an object anywhere is also in my opinion a weakness that can lead to sloppy style, and I know this from direct experience.

So my conclusion is this. If I never write another line of Clojure code after my current project, my time will not have been wasted. Clojure requires a different way of thinking. It never hurts to take a different point of view, especially with problem solving. However, I think more Clojure projects will be forthcoming.