Archive for July, 2009

Fractal landscape

Time for an algorithmic interlude.

I’ve been playing with some code to generate a fractal landscape, and it had a flaw in it that was one of the more insidiously subtle bugs I’ve come across.

The classic way to do a fractal landscape is the diamond-square algorithm, but (for reasons I’m not revealing for now… ooooh, drama) I decided to do something different. The high-level algorithm is this:

  • Start with a small grid (say, 10×10) of uniformly random values. (The value will be the height of the landscape at that point.)
  • Repeat until big enough:
    • Expand the grid horizontally by one square as follows:
      • For each row, insert a new, empty square at a random location. Shift the squares after it to the right.
      • Set each new square’s value to the average of the four neighbouring squares, plus or minus a small random jitter. The landscape should be a torus (i.e. the right side wraps around to the left, and the top to the bottom, so that it can be tiled), so include squares on the opposite edge in the average if necessary. Disregard any neighbours that were newly created in the previous step.
    • Do the same thing vertically.

There’s some extra tweaking and smoothing and whatnot, but that’s the basic idea.

Now… what’s the flaw? There’s a prize* for the first person to find it.

* Prize not guaranteed to have material value or physical existence. Prizes will only be awarded for the particular flaw I have in mind, not any other flaw, whether I’d noticed it beforehand or not.

No comments

On having one’s cake and eating it too

Over the last little while I’ve made a few posts to the effect that I’m trying to come to terms with Common Lisp.

I still maintain that it’s a brilliantly powerful language, but I keep tripping over practical obstacles whenever I try to do anything useful with it:

  • While the syntax is beautifully suited to its macro system, it’s a pain in the butt for everything else. Either the flood of parentheses or Polish notation would probably be fine on their own, but together they’re quite a mouthful.
  • The names of a lot of things are reminiscent of times when opaque strings of letters were considered good programming. setf – WTF? = and eq and eql and equal and equalp that might all do different things, depending on the implementation? Seriously?
  • All the available compilers suck in at least one way. As far as I can tell, there’s no free (as in whichever) Common Lisp compiler that (a) can cleanly compile to an executable (without statically dumping the universe into it), (b) does threading properly, and (c) has a well-supported Windows port. In the learning stages none of those are critical, but it bugs me that at some point in the future I’m either going to have to do without portability and whatnot, or write my own compiler.
  • The REPL and SLIME and all that are great, and I do like the idea of interactive programming, but there are all kinds of weird quirks about it. As a quick example, SBCL (the compiler that comes closest to handling all the issues I just mentioned) prints pages of warnings every time I compile after the first time, because I’m redefining all my functions. I couldn’t find a way to turn this off – I’m sure there is one somewhere, but it’s just offputting.

I could probably barge through a lot of these problems, and some of them are probably more about my preconceptions from programming in Algol-family languages for something like 20 years, but as a whole they just make me depressed about trying to approach it. Which is not good, because it doesn’t take much to get me to procrastinate, and then I just sit there feeling lazy.

A couple of months ago I read a bit about Scheme, which is the other main Lisp variant. On the surface, it seems to have more sensible naming, less frustrating compilers, and a cleaner design. On the other hand, it (apparently) has a less powerful macro system, weaker support from third-party libraries (which is saying something), and leaves out a lot of features in the name of minimalism. Scheme vs Common Lisp seems to be one of those vi vs emacs sort of religious wars – see this Stack Overflow question for example.

So fuck it all. Lisp shmisp. (Or maybe lithp.)

I’m doing this to learn practical skills and keep my brain ticking. In a perfect world, a programming language would get out of the way and let me do what I want without bringing all its baggage along. Maybe that’s asking too much, I dunno.

So my Lisp education is going on indefinite hiatus. Instead, I’m going to wade back into the world of Haskell. Haskell was the first language I studied at university, under the great Richard Buckland (more on that later), and I feel like it’s time for a revisit, this time from the perspective of Real Programming rather than as an educational tool.

No comments

Sick-ish

DMM wrote this not so many hours ago:

Feeling slightly… but not very… sick. A tiny bit of a cough. A little weak in the muscles. But no fever or runny nose or anything. …

It doesn’t seem to the swine flu. Fingers crossed.

…which is exactly the same boat I’m in. Staying home today as a precautionary thing.

No comments