Log in

No account? Create an account
Programming - Arvind Narayanan's journal [entries|archive|friends|userinfo]

Programming [Oct. 13th, 2006|02:08 pm]
Arvind Narayanan
[Tags|, , , ]
[Current Mood |happyhappy]

I think I've started to reach the point where I view the programs I'm writing as functional by default, rather than continually retrospectively realizing that the complexity can be greatly reduced by functionally remodularizing the code.

I believe this is a significant event in the personal journey of any programmer, kinda like understanding for the first time how you can save hours of frustration by not using goto's. It involves a kind of intellectual transcendence, in the sense that you can't really appreciate what it's like to be there without actually being there.

Paul Graham said it best:
As long as our hypothetical Blub programmer is looking down the power continuum, he knows he's looking down. Languages less powerful than Blub are obviously less powerful, because they're missing some feature he's used to. But when our hypothetical Blub programmer looks in the other direction, up the power continuum, he doesn't realize he's looking up. What he sees are merely weird languages. He probably considers them about equivalent in power to Blub, but with all this other hairy stuff thrown in as well. Blub is good enough for him, because he thinks in Blub.

When we switch to the point of view of a programmer using any of the languages higher up the power continuum, however, we find that he in turn looks down upon Blub. How can you get anything done in Blub? It doesn't even have y.

By induction, the only programmers in a position to see all the differences in power between the various languages are those who understand the most powerful one. (This is probably what Eric Raymond meant about Lisp making you a better programmer.) You can't trust the opinions of the others, because of the Blub paradox: they're satisfied with whatever language they happen to use, because it dictates the way they think about programs.
I'm probably never going to learn to think in Lisp, mostly because I don't have the time, and because I don't think I can do without python's library support. Now and then I run into the limitations of not using the most powerful language available, such as when I find that I really need to use macros. I'm reminded of a Calvin quote: "I imagine bugs have a dim perception that nature played a cruel trick on them, but they lack the intelligence to comprehend the magnitude of it".

On a more pragmatic note, python's performance is killing me. I keep hearing about some superfast implementations but AFAIK they're still vaporware.

(Deleted comment)
[User Picture]From: arvindn
2006-10-14 06:30 am (UTC)
I mean functional in the sense of functional programming.
(Reply) (Parent) (Thread)
(Deleted comment)
[User Picture]From: arvindn
2006-10-14 05:13 pm (UTC)
Oh, sorry. Functional programming has some characterstics that are very different from procedural:

* Immutable state
* Higher order functions
* Recursion
* Laziness

In lisp, these are all taken to the extreme. Your model of computation is not a Turing machine (or RAM machine) any more, but a lambda calculus machine. You get a level of modularity that's very, very hard to reproduce with OO, even in theory, and impossible in practice. You should take a quick look at http://www.cs.chalmers.se/~rjmh/Papers/whyfp.html

Functional programming is also a state of mind. I mentally cringe when I overwrite a variable, unless it's something trivial and easy to reason about like a counter; even then I try to avoid keeping explicit counters and count the length of objects when needed instead. Where earlier I would have copy-pasted code, now I would make a function out of it (even if it is just 1 or 2 lines) and pass it around. Recursion doesn't fit naturally into what I'm doing (large datasets) but I still use it more than your average Java programmer would. Finally, I use laziness to the extent that python makes it comfortable, such as by passing around iterators. This is what I'm so elated about.
(Reply) (Parent) (Thread)
(Deleted comment)
[User Picture]From: haran
2006-10-14 04:13 am (UTC)
python's performance is killing me. I keep hearing about some superfast implementations but AFAIK they're still vaporware.

This is supposed to be pretty mature.
(Reply) (Thread)
[User Picture]From: arvindn
2006-10-14 06:44 am (UTC)
Thanks for the suggestion. But the last time I tried it, I spent an hour trying to get hello world to run and then gave up. In the open source world, software is considered to work if it runs on the developer's machine. Or ran once on the developer's machine. After supplying the right config files.

If psyco is really mature, it should be in fedora's repository. I think I'm going to put up with the slowness until then.
(Reply) (Parent) (Thread)
[User Picture]From: reubgr
2006-11-14 03:27 pm (UTC)
You're looking for Psycho:

It JITs python to be really fast.
(Reply) (Thread)
[User Picture]From: reubgr
2006-11-14 03:28 pm (UTC)
My bad -- didn't see earlier comment.
(Reply) (Parent) (Thread)
[User Picture]From: arvindn
2006-11-14 03:35 pm (UTC)
Actually, have you gotten psyco running on the school computers? If so you can help me :)
(Reply) (Parent) (Thread)