Wednesday, September 12, 2007

The Joy of Recursion

I read Why Functional Programming Matters because Raganwald told me to and he's about a jillion times smarter than I am. It was an equally frustrating and enlightening experience for me, but boy howdy has it ever gotten my brain wheels spinning again.
For too long, I think I've forgotten why I got into programming - there's a terrifying amount of stuff to learn. As of late, I've settled into a rut (however productive) of kind of reflexively seeing every problem in terms of objects for the business logic and a relational database to persist the data. It's good and bad - I can make it work, but there's that nagging little voice in the back of my head wondering if I could have done it differently (and the slightly louder voice reminding me to ignore that voice and Do The Simplest Thing That Could Possibly Work and map and filter aren't necessarily it (for me (right now))).
Back in Ye Good Olde Days, I really got a kick out of programming. Moving at my own pace, figuring out what worked and what didn't at my own pace... what could be better? One of the things I figured out back then was recursion sucks and no one should use it ever. Asking me why that got stuck in my head would be as productive as asking why I didn't go outside, it's a beautiful day. Computer's inside. But the idea that I could get through life with nothing but recursion did stick with me for a few years. In my defense, I had a lot of awful ideas about how programming should and shouldn't work back then. Some things never change, I guess.
After my limited experience in college with Lisp, I chalked it (Lisp/functional programming) up as something destined to die in the halls of academia. After all, object oriented programming has conquered in the marketplace of ideas, right? Well, yeah.
But then again, there's a not-so-small matter of The Multicore Era. I've done my penance with threading and there was little with it that came easy. I still think that there must be companies out there working on C++/Java/.Net compilers that hide the ugliness of working with multiple CPUs/cores, but curiously, the same machinations you go through to make your object oriented code play nice with multiple processors make it start to look... almost functional.
Why FP Matters did a good job of opening my eyes and stretching out corners of my brain that have laid fallow for too long, but it's aggravating. For every idea I can almost wrap my head around, there's a dozen that I can see on the periphery slipping away from me.
I don't want to let those dozens slip away from me quite yet. I'm now slowly working my way through YAHT (Yet Another Haskell Tutorial, a better read than the name would have you believe, honest) and rediscovering my inner child. I'm inept in Haskell - functional programming's still completely foreign to me. The syntax feels awkward. I'm not even illiterate in this and I can still feel myself trying to translate OO to FP.
But there's something to be said for rediscovering the Fibonacci sequence and factorial functions in a new light. I shouldn't be beaming this much about a dead academic language. I shouldn't seriously be considering reading The Structure and Interpretation of Computer Programming over the winter. I should maybe be making friends with a nice language like F# that I could do something with. How will I ever make money with this?
There's no real logic to it, I just feel like the time's right to play with it (don't I have a Wii for that?) and I'm enjoying it. My inner child's even beginning to make peace with recursion.

5 comments:

Anonymous said...

F# may be nice for people with an OO mindset, but I learned Haskel because it's a pure functional language and will reshape your mind more than F# will. And Haskell is certainly not a dead academic language.

Peter said...

To sum up why Haskell's worth it:

fibs = 1:1:zipWith (+) fibs (tail fibs)


That's a definition of the list of all fibonacci numbers.

All of them.

And that's a useful construct in a program that needs the fibonacci numbers. Not only does it define all fibonacci numbers, but it does so in a useful way.

Dave Solomon said...

I just popped that fibs function into hugs and it did stuff and I have no idea how but I am totally pumped to get to the point where I can unravel the magic.
I'll probably never do anything productive (aside from go through math problems in Project Euler) with Haskell, but that's OK because it feels like I'm learning a magic trick when I get into it.
Enough with the spreadsheets, it's time to rediscover some wonder.

Anonymous said...

I actually liked recursion and have had the temerity to use it in commercial code on more than one occassion (taking care to avoid stack overflow and the like, of course). However, I fear the majority of programmers, certainly over half, just don't think that way. That's a problem for the Haskells and Erlangs of the world.

There are other Multicore Answers out there:

http://smoothspan.wordpress.com/2007/09/19/and-now-for-something-completely-different-visual-programming-cracks-the-multicore-barrier/

Cheers!

BW

James Orman said...

Recursion eats up ram. Not good.