[Haskell-beginners] How to think in Haskell

Tommy M. McGuire mcguire at crsr.net
Thu Dec 16 18:32:22 CET 2010


On 12/16/2010 08:46 AM, Jun HU wrote:
> My first language is C, and I've strong intention in learning a pure
> functional programming language. My very first question is how to
> think in the functional programming way, anyone has some ideas.
> Really appreciate.... 

I cannot claim C as my first language, but I do say it is my "natural"
language; I have written more code in it than I have in any other, and
I've been using it for nigh on 20 years.

I also cannot claim to think in the functional programming way, but it
definitely has changed my thinking. (Thinking about it now, I believe
functional programming has had a bigger effect than I had considered.
But I am definitely not a Haskell guru.)

I believe the biggest difference between functional programming and
procedural is thinking equationally rather than operationally. With
procedural programming as in C (and object oriented languages, which in
my experience are more procedural than not), the tendency is to think
operationally: "First this happens, then it does this, then that,...."
Now, back at UT Austin (I grew up in the land of Dijkstra), at the time
anyway, they were fond of axiomatic thinking: "At this point in the
program text, the state is this; at that point in the text, the state is
that...." That is definitely a significant improvement, and I attribute
what decent code I have written to that approach, but it definitely is
not as natural as either operational or equational thinking.

In Haskell especially, but in other functional languages to a lesser
extent (and also when doing functional approaches in a non-functional
language), it is very important to think equationally: "This means that;
this other thing is that...." Now, equational thinking is easy for stuff
like

 add_one n = n + 1

But it is not immediately apparent how that extends to something that
needs recursion, say. The big "Ah-ha!" moment for me is the realization
that recursion and mathematical induction are the same thing (ok, it's
not a surprise; the big deal was when I internalized it). With that, you
can deal with anything equational and recursive as equational and
inductive. The change in thinking is as big as the difference between
operational and axiomatic.

Equational thinking extends beyond basic function definitions; I think
it is the key to the type system, classes, and most of the other neat
language features.

At this point, I am still struggling with category theory, although I
have been able to make use of monads reasonably elegantly (in my
(humble) opinion, of course). I think there is another step there,
although I haven't made it. (I am willing to change my traditional
definition, "Programming is applied formal logic," to include at least
abstract algebra and category theory at this point, though.)


-- 
Tommy M. McGuire
mcguire at crsr.net



More information about the Beginners mailing list