Archive for September, 2010

Sep 29 2010

Falling asleep

Published by Dougal under Family, Humour

D: Did I send you the link about the woman who was blogging everything her husband said as he fell asleep?

H: No! Did he say something about being hacked by 2012…


(About leaving first thing in the morning before I am awake.)

H: I see you there all asleep and not even moving and I worry that there’s something wrong with you and then you open your can of Coke…

D: Buahahaha!

H: What? Oh. There wasn’t a security barrier either, was there? :-(


H: I see what you’re doing there. I see what you’re gathering.

D: What?

H: You’re going around gathering … recipes …

3 responses so far

Sep 27 2010

Mapping constellations to months.

Published by Dougal under Programming

I recently had cause to write a function from dates to zodiac signs, and couldn’t see any succinct way to do it. (Don’t worry, I’ve not gone soft in the head. It was just an exercise.)

The way the constellations are split across the months meant I just brute-forced the answer, which wins no points on either elegance or readability. This is not the kind of code which could be described as “obviously correct”:

function get_starsign($month, $day) {
    switch ($month) {
    case 1: // january
        if ($day < 20) {
            return 10;
        } else {
            return 11;
        }
    // ...

So what do you think? Some points:

  • There are twelve signs of the zodiac split across twelve months, but they span the end of one month and the start of the next.
  • The start date in each month is not obvious or consistent. It ranges anywhere from the 19th of the month to the 23rd.

It occurs to me that maybe the important data is the cutoff dates. Assuming months are zero-indexed for ease of calculation, we can order the cutoff dates accordingly:

data Month = Jan | Feb | Mar | Apr 
           | May | Jun | Jul | Aug 
           | Sep | Oct | Nov | Dec
           deriving (Eq, Enum, Bounded, Show)
data Sign = Capricorn | Aquarius | Pisces | Aries
          | Taurus | Gemini | Cancer | Leo
          | Virgo | Libra | Scorpio | Sagittarius
          deriving (Eq, Enum, Bounded, Show)
cutoffs = [20,19,21,20,21,21,23,23,23,23,22,22]
 
sign :: Month -> Int -> Sign
sign m d = toEnum (if d < cutoff then ix else (ix+1`mod`12))
 where cutoff = cutoffs !! ix
       ix = fromEnum m

Yes, in hindsight something like this would have been nicer. The non-algorithmic part of the problem is contained in the one list, cutoffs, rather than spread across a series of return statements.

Comments Off

Sep 24 2010

Football and zombies (high-brow literature)

Published by Dougal under Books, Reviews

It’s a “short month” this time between book group meetings, so I didn’t take anything very challenging. I’ve also still got North and South (Elizabeth Gaskell) which I’m making some sort of headway with. I was told it gets better from the opening few chapters, so I’ll let you know. Last weekend I picked up a few very cheap paperbacks in Barnardo’s on Leith Walk. I think it was about £1.50 for Unseen Academicals, Whisky Galore! and The Three Musketeers which I like to think was a pretty decent bargain.

Pride and Prejudice and Zombies

So in the last month I’ve read:

  • Pride and Prejudice and Zombies, the Graphic Novel. This is my book-group book, which is really fun. I think you’d have to be familiar with the original original to enjoy the tribute. Essentially it’s Austen’s classic with heavy zombie/kung fu influences. (I bet you didn’t know the Bennett sisters trained with Shaolin monks, did you?) The artistry was a bit disappointing, and confusing in some parts, but the wit of the language married to the absurdity of the Undead more than made up for the visual flaws. I’m very tempted to read Pride and Prejudice and Zombies the novel now, to make the most of the bits I enjoyed.
  • Unseen Academicals (Terry Pratchett). The city of Ankh-Morpork discovers football — and good quality pies — in good style. Short(crust) and easy to digest, and very enjoyable.

Comments Off

Sep 11 2010

Content decrement register

Published by Dougal under Computing, Programming

This is going to seem like a point-out-the-obvious post, where I go to great pains to point out just how very wet water can be, but I assure you, this is new enough to me that I had to reach this conclusion.

Okay, so Scheme typically works on the everything-is-a-tuple philosophy. Other structures can be built out of nested or chained tuples. A tuple, or pair, looks like this:

(1,2)

Immutable linked lists can be made by inserting one pair inside the second part of another pair, with an empty marker at the end.

(1,(2,null))

The functions for accessing the left and right elements in a tuple are called, for historical reasons, car and cdr. Applying car to (1,2) gives you the 1, and applying cdr gives you the 2.

When applied to the linked list implementation, car and cdr are no longer really left and right but head and tail. And so for the past few months working on SICP I’ve been thinking about car as head and cdr as tail and mostly getting away with it.

But sooner or later the inaccuracy of this correspondence was going to become clear, and the other day it did. Single linked lists point from the head to the tail but not in the other direction. From each element you can delve deeper but you cannot move higher up the chain. An obvious reason for this is that there may not be a single “higher up” to link to. Immutable list tails can be shared.

Scheming

Double linked lists can be traversed in either direction. If you’ve only got two places to store something (left and right elements in a pair) and three things to store (the left side of the list, the right side of the list and current element) you need to double up somewhere. At which point it is necessary to remember that cdr isn’t the same thing as tail.

Which makes me wonder why the idiomatic way of traversing lists is with car and cdr? Why is the meaning of tail intentionally blurred with the meaning of cdr? SICP puts a lot of emphasis on abstraction of implementation from intent yet it still litters its list-manipulation code with car and cdr, references to the underlying representation that doesn’t have anything to do with the meaning of the lists at all.

Of course we may as well ask why cdr, an acronym for “Contents of the Decrement part of Register number”, itself is a reference to an implementation detail on a 1950s computer. I’m sure left and right or something slightly more meaningful wouldn’t have hurt.

4 responses so far

Sep 11 2010

How to bake naan bread without a tandoor

Published by Dougal under Baking, Food

If you’ve never seen real naan made in a tandoor it’s hard to imagine the strangeness of the procedure. Stretching out a thin piece of dough and attaching it to the walls of a dangerously hot clay oven for a few brief minutes. But it works marvellously to bake thin bread like naan, and produces a unique combination of smooth even flatbread on one side and charred blisters on the other.

The method I used was a combination of Madhur Jaffrey’s recipe and Heston Blumenthal’s baking technique. Heston Blumenthal, for reasons best known to himself, uses a chemically-leavened dough which seems more suited to scones than bread so I didn’t bother with his recipe. But the technique of heating two baking stones face-to-face in the oven seemed worth pursuing. You use one stone for baking the bread while the other reflects heat onto the outside of the naan. If your oven can do it you could experiment with the grill at the same time for extra direct heat.

Slapped onto the baking stone

I ran out of time to let the bread rise properly when I tried this recipe, which resulted in a stodgier less elastic product than I was hoping for. But the fact that naan is spread extremely flat before baking means it’s quite resilient to under-proving: in essence it becomes a slightly fluffier unleavened bread like a pitta or something.

Home-baked naan

The baking was great fun. I had my two pizza stones face to face in the V shape prescribed, the temperature of the oven at maximum, and a tea towel with my teardrop shaped naan sitting on it. Real tandoor chefs seem to use a small cushion to attach the dough to the walls of the tandoor, so the tea towel was a good approximation which worked really well. I could push the dough hard onto the stone and it would stick firmly. After 3–4 minutes I pulled them out and put the next one in. The flat side was evenly brown and the bubbly side had a nice organic texture and browned bubbles. Pretty authentic looking.

Ready to go in the oven

It tasted quite nice, and the sour note of the yoghurt came through quite well. Less time restrictions would have produced an even nicer texture, I’m sure. My next foray will involve flavourings, which I had neither the energy nor time to investigate on this occasion. Garlic looks like a good place to start.

3 responses so far

Sep 02 2010

Three books: Swedish crime, allegorical tales and first-person shooters

Published by Dougal under Books, Reviews

Last book group I picked up two books but didn’t get round to reading either for ages. The second one, North and South by Elizabeth Gaskell, is proving too difficult so I might just abandon it where I am. I certainly don’t think I’ll get it read by next weekend.

What have I read then?

Three to See the King (Magnus Mills) is a strange tale about a man who lives in a tin house on a desert plane, a mile or so away from other people who live in their lone tin houses. The story follows the fate of a grand excavation, with thousands of people trying to build a new township with tin houses. The prose is very flat and the humour dry and deadpan. Whatever I picked up from this book I’m sure I missed most of it. It was interesting, though really I’m not sure if I recommend it.

Pandaemonium is Christopher Brookmyre’s latest homage to computer games.

Doom Install Disks

The premise of Doom is relocated from the moons of Mars to the wilds of Scotland (but of course…) and the lone marine replaced by schoolchildren, teachers and a couple of Catholic priests. And it’s great.

The Girl with the Dragon Tattoo (Stieg Larsson) is pretty popular these days. This and the two sequels seem to sit permanently on the Amazon bestseller list. I can only partially see why. The story is fairly involving, with a few interesting mysteries unravelled by the end, but the writing is absolutely atrocious. Most of the time it’s not descended to the Dan Brown level but occasionally the tone of the prose is so badly off-kilter it makes you wonder if it was written by a native speaker. Of course, it wasn’t written by a native speaker, so the real question is whether this wooden and lumpen writing is an accurate reflection of Larsson’s Swedish source text, or whether the translator was just an idiot.

2 responses so far