Tuesday, 27 November 2007

One Computer

Thomas Watson from IBM said that he could foresee a need for perhaps five computers worldwide, and we now know that that figure was wrong, because he overestimated by four. - Clay Shirky

There is only one computer in the world now, and that computer is the World Wide Web. Developers must know that, understand that and believe that.

Software does not matter unless it is written to run on that one computer. Know that too.

I have been saying this for awhile now, and some people have had some trouble understanding what I mean. I am not saying that the only thing that is to be written must be web applications. What I am trying to say is that if you are writing software it must be able to be used by applications and software that does run on the Web.

If your software does not, then it will simply be ignored. That's not malicious or intended as a criticism of your code, I'm sure it's very good. Think about it this way: as OS/2 faded away any software that only ran on OS/2 simply stopped mattering. This is just happening again, except all operating systems are fading away, replaced with that one computer.

So, if you have some amazing idea for writing some beautiful video processing code in the current functional-language-of-the-moment, you must have a plan for making that code useful to the web otherwise no one will ever use your code.

Positive Change

There was a federal election in Australia, just last weekend: the 24th of November. This election went a lot better than the last few years worth of elections. John Howard finally lost. After 11 years in power and four election victories against either ineffective or insane Labor opposition, the mean, evil little troll has finally been ejected from power.

It was very satisfying to also see him so thoroughly ejected from power: only the second sitting prime minister to lose his seat. Ahhh...

And Australia can now try to once again become a better place to live.

There has been much discussion over why Howard deserved to lose. Many have correctly pointed out that for all his claims he was not a leader who championed much reform; that the continuous 11 years of economic growth was really part of a longer 16 years of growth started under the previous Labor government due to actual economic reforms; that he no longer offered any kind of vision for the future of Australia; that his threat to hand over to Peter Costello was seen as just that: a threat; that it was just his 'turn' to go.

For me, while all of those (except the last) are true, none come close to the real reason why I have always been adamantly against Howard, and why I am particularly happy that he has now been voted out.

Howard was a leader who created a government of isolation, selfishness and meanness. And of course this sort of attitude at the level of the federal government can't help but affect the entire shape and direction of Australian society.

Consider a handful of events under Howard's watch:

  • Pauline Hanson spewed her racist, xenophobic vitriol without any form of condemnation from the government, permanently damaging our image and reputation throughout Asia.
  • Refugees aboard the MV Tampa were prevented from entering the country, extending our damaged reputation to the entire world.
  • The GST, a fundamentally inequitable tax hated by both small businesses and the poor alike, was introduced. When there was apparently no need as all the earnings were returned as tax cuts.
  • Children nearly drowning while on their way to the Australia was used as a wedge to further incite anger against refugees.
  • A deeply unpopular war was started with the sole justification of supporting George W. Bush.
  • Tax cuts were the sole mechanism of delivering benefits to the population: a mechanism that always favours the rich over the poor.
  • University educations became a mechanism to turn a profit.
  • The enormous earnings from the global resources boom was squandered without any investment in social services.
  • The Australian republic movement was deeply damaged.
  • There was absolutely zero progress on Aboriginal reconciliation: capped with the 'intervention.' Itself worryingly reminiscent of the Stolen Generation.

Ultimately, this was a government of the mean to benefit the rich. Under Howard and his cronies Australia became a more selfish country, a more racist country, a country of people who would not do something just because it was the right thing to do.

I was always against Howard because of the damage I thought his ideaology was doing to the fabric of our society. In the end, my theory is that he lost because people finally saw him for what he was. [WorkChoices] showed that he didn't really care about his 'battlers.' And also, I believe that people didn't want to be selfish and scared anymore. The country wanted a more positive view of the future. You can only be terrified of your neighbour for so long before you decide that it's enough.

It is interesting to see that others are also coming to this conclusion. My fervent hope is that his legacy is seen for what it really is. In the end, the country will be a better place without him. Hopefully, it won't take too long for the damage to be repaired.

Good riddance Howard, we're better off without you.

Monday, 19 November 2007

Currying

As a lazy functional language Haskell has to provide automatic currying of all functions. But once you've tried to use a functional language that doesn't automatically curry you realise this isn't just an implementation detail, it's a useful technique for concisely implementing programs.

but before describing how currying helps, what is it? Currying is a concept from the analysis of mathematical functions. Say, you have a function f(a, b), that takes two parameters and returns a value. Currying reduces this to two functions: one that takes the other parameter and returns the original value.

That is, currying reduces functions parameter-by-parameter into other functions - these new functions each take one less parameter, but return a function, instead of a value, that then takes the remaining parameters.

For example: f(a, b) = a + 2b. If this were curried with respect to a = 5, b = 10, then the function would be reduced as follows:

f(5, b) = 5 + 2b
        = f(b)
f(10) = 5 + 2 x 10
      = 25

And hopefully you noticed that halfway through that evaluation we found a function over b which had already had a value substituted in for a. This, by the way, is also an example of partial evaluation. In fact, it's more an example of partial evaluation than currying, as currying does not require values for function parameters.

Anyway, that's a brief introduction to currying in maths, but I'm not a mathematician so for some more (accurate) information try reading up on combinatory logic.

So, how does currying exhibit in Haskell? Function application is treated as a special operation, distinct from calling a function. Functions are applied from left to right, to the first parameter, producing a new function that is applied to the second parameter and so on. A Haskell function call like:

f 1 10 "Hello"

is evaluated as if it's parenthesised like:

((f 1) 10) "Hello"

If working from left to right each additional expression produces a new valid expression, then does that mean we can leave off an arbitrary number of of expressions from the right and still have a valid expression? Yes, that's precisely true. In fact that would actually be a function which could then be given a name and provided with any number of values.

And this is where currying really comes in use for expressing code. Using the right functions and operations you can typically define your own functions without naming any parameters.

In particular, this dramatically reduces the number of anonymous functions you need to declare. As opposed to Scheme, they become quite rare.

A Scheme implementation of reverse could look something like:

(define (reverse lst)
  (define (r lst)
    (if (null? (cdr lst))
        (lambda (t) (cons (car lst) t))
        (lambda (t) ((r (cdr lst)) (cons (car lst) t)))))
  ((r lst) null))

While an equivalent Haskell implementation could look like:

reverse lst = r lst []
  where
    r (h:[]) = (:) h
    r (h:hs) = r hs . (:) h

Of course, in the languages' standard libraries these functions are defined quite differently (Haskell's is only one line...) But this is useful as those are equivalent implementations - Haskell's is shorter only because it automatically curries.

Finally, currying is required in Haskell because it's a lazy language. The currying produces a train of anonymous functions - each retained as a value. Arriving at a result when required is simply achieved by evaluating those retained anonymous function values. But only the minimum set that are actually required.

Understanding currying and being able to curry your own functions is your head is a fundamental step to 'getting' functional programming.

Bear With Me...

Now that I'm using some proper tools for my blog editing I'm going back through my old posts making sure the formatting and HTML are correct. In particular, I've turned off the Blogger setting to do magic with line breaks, and instead I'm manually inserting <br/>'s where I want them to be.

This is better for my general OCD nature regarding all forms of code I write, and I hope my blog will look a bit better too. Please bear with me as all my posts slowly get cleaned up...

Oh, and if there are any other Mac Emacs users out there who are interested in my little tool, send me an email and I'll try to arrange to share it.

Sunday, 18 November 2007

Local Editing

I was never entirely happy with using the Blogger editing box in a web browser for an entire, long post. It's in a browser, where the slightest click or accidental action could cause the post to be lost. Plus, all my blog posts are now stored on Google's server. Call me old-fashioned, but I like my work to be on my computer.

Plus, I'm an Emacs user, I want to edit my work in that editor/operating system/religion. Call me very old-fashioned...
Well, I've finally found a solution. The blog editing software from Red Sweater, MarsEdit, will quite happily deal with blogs on Blogger: downloading old posts and uploading new ones, unlilke some other blogging clients I've tried.

Though it has the ability to launch other editors, like TextMate, to edit posts this does not work immediately with Emacs. MarsEdit doesn't ever notice that Emacs eventually closes the file. The MarsEdit developer was very helpful. He pointed me to a page describing what MarsEdit is waiting for, and after a bit of Cocoa hacking I can now edit my posts in Emacs. Woohoo!

Well, that's assuming this post appears OK...

And as an aside, I'm pretty impressed with Xcode 3.0. It's got some pretty nice features: the purtiest were these colourful labels that appear directly in your source marking compiler errors and warnings.

Any way, assuming this appears correctly, I'll be buying MarsEdit, switching to that for blogging, and you should see a lot more posts out of me in the future.