Wednesday, June 23, 2010

A Half-Minute Introduction to Functional and Object-Oriented Programming

Okay, I'm not going to be quite so ambitious as to explain everything about both paradigms, just one example that highlights what a major difference is and why it exists.

Today's example: "object" state.

Imagine a small Post-It pad as your memory, with, say, a number "2" written on it. In fact, let's be more "ambitious," and imagine a mini-clipboard, with four post-it notes on the back:


"John"                "Smith" 

Yes                   2       

Now, imagine tearing off the "2", writing a "3" on a new note, and sticking the new one in the place of the "2".
To an OOP person, you've just changed the state of an object. To an FP person, you just changed which state the object refers to. The OOP person maintains continuity with the object. The FP person keeps continuity with the values associated with an object. To the OOP person, it's the same object, just with different (new) parts. To the FP person, it's a different object, because it has different (new) parts (it just has the same name as the original object).

It's kind of like marriage: you're still you (OO), you're just now the same as you used to be (F)...

Monday, May 24, 2010

Dabbling in incremental folds



    I was playing around coding up programs for Project Euler, when I noticed that I was 1) using an old stand-by function, to add up the digits in a big number; and 2) I was coding it up in a way that echoes up other functions I’ve done, such as the ever-popular “chunk”; 3) I was not coding it up in the semi-unrolled-in-another-dimension form of “map snd . takeWhile (p.fst) . iterate (\(old, pair) -> (new, different pair) $ start” that I also use for that kind of problem, but in the “let (intermediate, pair) = auxiliaryFunction argument in finalProcessing intermediate (recursiveCallOn pair” format. I asked about it on #haskell - hands down, the best resource for help on all things CS-ish EVAR! - when for some reason my brain zigged instead of zagged, and I recalled my Internet “colleague” (make no dispute: the quotes are there because I have no more qualification to be this man’s colleague than I do Stephen Hawking) Sean Leather. Specifically, his blog post http://splonderzoek.blogspot.com/2009/04/latest-on-incremental-fold-and.html  where he speaks writes some more about incremental folds, and like a corny Windows 7 commercial, it hit me: I’ve been writing (and re-writing) incremental folds!

    I find this to be a big deal, if only because I have finally hit the level where instead of just reading about all of these useful high-brain-power techniques, I have caught myself using one - yes! I may not be the sharpest tool in the shed (see: Cletus), but love it all, I Am Trainable!
So, knowing a good thing when I see one, I immediately...well, I immediately got back to doing the day job that I’m really supposed to do, instead of goofing off, then I spent some time playing around on IRC, but that very day, I went back to re-read about incremental folds, and how what I was doing fit all into it. Chalk one up for goofing around on the Internet!