Monday, June 1, 2009

(Re)Thinking Functionally

Last Saturday, I attended the very vibrant and well-put-together Chicago Code Camp. A couple of the sessions I attended were by Dean Wampler. They were both about Functional Programming (in Ruby and Scala, respectively). 

To me, the most significant transition towards functional languages is the art of thinking functionally. Or, re-thinking functionally − since when I started programming, C was the rage and functions were all we had. Thinking back, we could have used function pointers as explicit implementations of closures (sort-of) rather than as a hack towards creating poor-man's-C-objects (= structs and its related functions in one C-file).

I suppose we were young then. Some of us, at least (the ones who were writing code in late 80s / early 90s, that is). We, who had grown from writing assembler code to the (slightly) more abstract syntax of C, followed the evolution of C-like languages.

It so happened that C (with functions and function pointers and structs) evolved towards C++ (with classes and objects) rather than towards a true FP language (with true closures − and perhaps a canonical implementation of lambda calculus).  

Which brings me back to my current challenge. After having spent the last 15+ years not only viewing the world around me in terms of classes and objects but also thinking that way, it takes some effort to (re)start thinking in terms of functions. 

Powerful analogies can help (or hinder) this transformation. For me, the turning point towards "thinking-in-objects" was when a college professor analogized the breaking of a glass when it hit the stone floor (but not when it hits a rubber mat) as the different interactions between the glass object, the floor object and the rubber mat objects:

shards = glass.strike(stone-floor);
glass  = glass.strike(rubber-mat);

I suppose I should restart thinking in terms of (pseudo) lambda function which transform the glass into either shards or the glass (= identity function) depending on the parameter:

F(m)(n) = shards, ∀ m ∈ {glasses}, n ∈ {stone-floors}
        = m,      ∀ m ∈ {glasses}, n ∈ {rubber=mats}
        = undefined, otherwise

I've been up this hill before, although it'll still take some effort to get to the summit again. I am happy and excited to be able to undertake the journey. 


No comments: