Archive for March, 2007

What’s in a syntax?

Saturday, March 31st, 2007

I was looking at JGA earlier. Basically yet another instance of the JGreenspun pattern. I have my own implementation of the same sort of thing - JGA looks significantly better than mine frankly.

But, even with a nice terse syntax, the kind of shenanigans one has to go through to make it work are pretty horrible. Take a look at some of my code snippets if you don’t believe me (granted the snippets I had are not very terse, but due to heavy use of anonymous inner classes one is pretty stuck with using constructors, which with abundant generics quickly leads to some really horrible lines). Although you can get most (well, about 75%) of the functionality of a functional language like ML or Haskell into Java if you hammer enough, the result is fairly mindly blastingly horrible.

Shorter syntax for declaration of anonymous inner classes would certainly help a bit, but I don’t think it’s really going to be enough. I think that until closures come along to Java (and I mean proper BGGA style closures. Anonymous inner classes just aren’t going to cut it) I’m going to have to revert to the way of doing things that doesn’t involve too much greenspun. And/or defect to Scala completely.

Unexpectedly popular

Saturday, March 31st, 2007

Hmm. In keeping with google’s recently acquired fascination with me, my stupid little parsec example appears to be the third from the top when you google for haskell parsec. I find this vaguely alarming.

re·cur·sion –noun: See recursion

Saturday, March 31st, 2007

I’ve just discovered the following interesting and related facts:

1) Chickenfoot’s pattern matching is very error tolerant. If it looks for “Older posts” and can’t find it, it will cheerfully settle for “Newer posts”.

2) StumbleUpon histories only go back a few months.

3) When writing code, you should be really careful to avoid infinite loops.

Sigh.

From the jargon file: Bogosort

Saturday, March 31st, 2007

bogo-sort: /boh`goh·sort´/, n.

(var.: stupid-sort) The archetypical perversely awful algorithm (as opposed to bubble sort, which is merely the generic bad algorithm). Bogo-sort is equivalent to repeatedly throwing a deck of cards in the air, picking them up at random, and then testing whether they are in order. It serves as a sort of canonical example of awfulness. Looking at a program and seeing a dumb algorithm, one might say “Oh, I see, this program uses bogo-sort.” Esp. appropriate for algorithms with factorial or super-exponential running time in the average case and probabilistically infinite worst-case running time. Compare bogus, brute force.

A spectacular variant of bogo-sort has been proposed which has the interesting property that, if the Many Worlds interpretation of quantum mechanics is true, it can sort an arbitrarily large array in linear time. (In the Many-Worlds model, the result of any quantum action is to split the universe-before into a sheaf of universes-after, one for each possible way the state vector can collapse; in any one of the universes-after the result appears random.) The steps are: 1. Permute the array randomly using a quantum process, 2. If the array is not sorted, destroy the universe (checking that the list is sorted requires O(n) time). Implementation of step 2 is left as an exercise for the reader.

fix f = let x = f x in x

Thursday, March 29th, 2007

One function in the Haskell standard library is ‘fix’, the least fixed point operator (a sort of generalisation of the Y combinator). As per the topic, the entire source code for it is:

fix :: (t -> t) -> t
fix f = let x = f x in x

This is really awesome, but it took me a while to figure out how it worked.

Let’s take an example:

g x = 1 : x
ones = fix g

This gives us an infinite list of ones. However, it’s important to note that at this point nothing is evaluated.

Suppose I wanted to evaluate head ones. How would the evaluation proceed? It would go as follows:

head(ones) = head(1 : ones) = 1

No more of the argument is evaluated than needed. That’s what laziness means.

Now let’s see how we use it as Y combinator.

genfact f 0 = 0
genfact f n = n * (f(n-1))

(I’m aware my bracketing is non-ideal. I haven’t quite got the hang of reading and writing Haskell in a normal style yet though)

fix genfact 3 = genfact (fix genfact) 3
= 3 * ( (fix genfact) 2 )
= 3 * ( genfact (fix genfact) 2)
= 3 * 2 * ( (fix genfact) 1)
= 3 * 2 * 1 * (fix genfact 0)
= 3 * 2 * 1 * (genfact (fix genfact) 0)
= 3 * 2 * 1 * 1
= 6

Neat.