Hobby-hacking Eric

2007-05-21

burn your gas factories down

I've been Haskelling for three years now and have been loving it. The only discomfort is that from time to time I become painfully aware of my habit of making things horrible and ugly. Consider this code for instance:

usine à gaz


List of random indices. It is a list [ rM, r(M+1), ..., rN ] where rX is a random number from X to N
> nexts g (l,h) = unfoldr nxt (g,l,h)
> where
> nxt (_,l,h) | l >= h = Nothing
> nxt (g,l,h) = let (r,g2) = randomR (l,h) g
> glh2 = (g2, l + 1, h)
> in Just (r, glh2)


This code has been called "clever". Anytime somebody calls your code "clever", you should begin to worry. I mean just look at that thing! You can positively hear the gears turning and the doodads clicking. This code is what the French call a "gas factory", something incomprehensibly and needlessly complex.

is there no simpler way?



Stop micromanaging! Just tell the computer what you want, not what you want it to do

> nexts2 g (l,h) = map (\x -> fst $ randomR (x,h) g) [l..h-1]


No gears or doodads. Geez... I don't what it is with me and making things more complicated than they need to be.


4 comments:

\-..-/ said...

Eric,

are the two really identical? The first one appears to use a changing RandomGen, while the second uses a single one for the whole map.

kowey said...

To be honest, I hadn't though about that (and still need to look into it more). Huh.

I also wonder why I didn't get any email notification about your comment. Huh. oh well.

In any case, if they are not identical, I'm guessing that I still want the second one anyway (any reason I should prefer the first?)

kowey said...
This comment has been removed by the author.
kowey said...

Hmm... indeed! Wait, so that means I don't need to feel bad about my gas factory? Or maybe there is a clear, simple and correct expression of what we want lurking around somewhere. Thanks!

It's kinda frightening... the type system protects me from making all sorts of gaffes, but somewhere, there is basic problem of being able to reason.

Sigh.