Category Archives: programming

Unexpectedly popular

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.

This entry was posted in programming and tagged on by .

re·cur·sion –noun: See recursion

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.

This entry was posted in programming and tagged on by .

From the jargon file: Bogosort

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.

This entry was posted in programming on by .

fix f = let x = f x in x

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.

This entry was posted in programming on by .