Archive for April, 2007

C: prefer f(…) over (*f)(…)


In C all function calls take place via a function pointer. It says so in section “Constraints: The expression that denotes the called function shall have type pointer to function returning void or returning an object type other than an array type.”.

So what happens when you write p = malloc(sizeof *p);? malloc isn’t a function pointer, it’s a function. What happens is that when a function is used in an expression it is automatically converted to a pointer to the function instead (section; except for sizeof where it is illegal, and & (address-of).

That means that if callback is already a function pointer you don’t need to go (*callback)() to call it, you can just go callback(). Most of the time I prefer that. It’s neater because you don’t need the * so you don’t need the parentheses either. It also shows confidence; it shows that you know what you’re doing.

If do you go (*callback)() then dereferencing the pointer yields a function, which is automatically converted back into the function pointer. It all seems a bit pointless. An amusing consequence, which not that many C programmers seem to realise, is that you can have as many *s as you like:


Frank C. Weiler asks me about emacs color


On 2007-04-29 Frank C. Weiler asks the following (on my about me page as it happens):

Dear DRJ11, I have been trying too long to locate where in the Emacs distribution a particular colour scheme is defined for a particular distribution that I have grown to like (Mandrake 10). I now run Fedora 6, but their solour scheme is black and white, and trheir window scheme sucks. I program in Fortran (all varieties), C, awk, and others, and like the fact that reserved words, constructs, etc, all appear in color, on a standard medium green background (soothing to the eyes) and a tan-yellow type set. Anyway, you seem to have found out all about Emacs colour, so can you point me in the correct direction? In really want to extract this colour scheme from this old version and tuck it away for all future invocations that I use.

Well, I don’t actually know much about emacs, and less about color. Did you read the bit in Stupid colour, stupid slime, stupid emacs where I say that I only use emacs for programming in elisp, info-mode, and inferior-lisp mode? I don’t use it for editing files (I do that in vi). Also, syntax highlighting generally annoys me so I usually switch it off.

So I’m not sure you’ve come to the right person.

Having said all that though, you might find the “frame parameters” node of the emacs info manual useful (C-h i d m emacs <RET> g frame parameters <RET> at least on my emacs 21.2.1 distribution that came with my MacBook).

And it seems to matter a lot whether you run emacs in tty-mode (inside a Terminal window, like I do), or as an X application (which is probably much more common these days).

Good luck.

8 legs better than 6


Today riding on an early summer heat-wave 3 wasps came into the house through our open front door and started buzzing about. One of them accidentally flew into the glass of the paraffin lamp and went an octave higher as it tried to escape. I don’t really mind the wasps, but the buzzing is kinda annoying after a while; usually I try and help them back out when the buzz gets too annoying.

No need to help the wasps today, all 3 wasps fell victim to two spiders that live in opposite corners of the window at the back.

Learning J – Part III


Please see Learning J Part I and Part II.


At the lexical level J is fairly unsurprising. A program consists of a series of sentences, one sentence per line (I think we can already see how the issue of breaking long lines never comes up). Comments are introduced with NB. and finish at the end of the line. Sentences are decomposed into words.

Examples words:


Names and numbers are pretty standard (but note that negative numbers are denoted using _, like ML, because - is a verb), but apart from those there is a bewildering array of punctuation available including items that are usually found as brackets, delimiters, and escapes in other languages.

There’s an additional quirk. A sequence of numbers separated by spaces forms a single word (a list). This only works for numbers. So 3 1 2 is a single word (a length 3 list), but a b c is not.

Each word is categorised into a part of speech. J deliberately uses terminology from natural language grammar.

  • Nouns: values to you and me. 1, 2 3 4, 'foo', and so on.
  • Verbs: functions or procedures. +, $, i..
  • Adverbs: monadic higher order functions. /, \.
  • Conjunctions: dyadic higher order functions. ", and perhaps lots more.
  • Punctuation: ( and ) (which have their conventional meaning) and maybe some more.
  • Copula: What J calls =: and =. for assigning names to things.

As we’ve seen, verbs can either be monadic or dyadic, so that’s: term verb term or verb term. I’m being deliberately vague about what a term is (mostly because I don’t know).

Adverbs are always monadic and follow the verb: term adverb.

Conjunctions are always dyadic: term conjunction term.

Let c be a conjunction, v be a verb, and a be an adverb.

There’s an obvious x v y v z ambiguity, as in 2^3^4. As previously discussed things group to the right, so this is 2^(3^4):

   2^3^4  NB. some number much larger than 256 ((2^3)^4)

Conjunction Adverb Precedence

What about v c v a? Is that (v c v) a or v c (v a)?

Let’s find out. Consider *: @ + / 1 2 3. There’s two new symbols I need to explain. Monadic *: is square (it squares its argument). @ is a conjunction called Atop, it’s a kind of compose operator.

So *: @ +/ 1 2 3 could mean either (*: @ +) / 1 2 3 which would fold the dyadic (*: @ +) over the list 1 2 3 or it could mean *: @ (+/) 1 2 3 which would apply the monadic *: @ (+/) to the list 1 2 3. (It could also mean *: @ (+/ 1 2 3), but it doesn’t.) The @ conjuction is defined so that when used dyadically (as in the first possible interpretation) x (*: @ +) y means the same as *: (x + y) (square the sum); when used monadically *: @ (+/) y means the same as *: (+/) y.

So the first interpretation would have the meaning 1 (*:@+) 2 (*:@+) 3 which is 676 (262). The second interpretation would have the meaning *: (+/ 1 2 3) which is 36. Let’s see:

   *:@(+/) 1 2 3
   (*:@+)/ 1 2 3
   *:@+/ 1 2 3  NB. Same as second example.

So we can see that the @ conjunction binds more tightly than the / adverb. Conjunctions have higher precedence.

Operator precedence is not enough

Sadly, whilst it’s tempting to think that you can parse J using an operator precedence parser, you can’t. That’s because the reductions to apply depend not on the syntactic category of the items being parsed but on their runtime class.

Consider what might happen if you had a conjunction whose result was sometimes a verb and sometimes a noun. This is extremely unconventional, but possible. I introduce the notion here to show how certain methods of parsing are not possible. I borrow from the future and show you my evil conjunction:

   ec =: conjunction define
if. n = m do. *: else. 4 end.

The evil conjunction ec takes two (noun) arguments and has the result *: (which is a verb) if they are equal, and the result 4 (which is a noun) if not. Now consider n ec m - 3. Is this ((n ec m)(- 3)) (monadic verb applied to - 3) or ((n ec m) - 3) (dyadic - applied to two nouns)? Actually it could be either:

   7 ec 0 - 3    NB. 7 ec 0 evaluates to the noun 4
   7 ec 7 - 3    NB. 7 ec 7 evaluates to the verb *: (square)

So the parsing of J is mingled with its execution. The way it actually works is that the sentence forms a queue of words. Words are moved from the right-hand end of the queue onto a stack. After every move the top four elements of the stack are considered and one of 9 possible reductions is applied. When no more reductions are possible another word is moved across to the top of the stack and the cycle repeats. This is all reasonably well described by the J dictionary appendix E.

It strikes me as a mix of elegant simplicity, stomach churning madness, and pragmatic considerations of actually implementing something on 1950′s hardware.

Next, part IV covers more syntax (trains) and introduces some more practical examples of conjunctions.



Improvement to Common Lisp:

Macro multiple-value-constantly

(multiple-value-constantly form*)

multiple-value-constantly evaluates each form, gathers all the values together, and yields a constant function that takes any number of arguments and returns the gathered values. It’s the spawn of multiple-value-call and constantly.

constantly could be defined as (defun constantly (it) (multiple-value-constantly it)).

Here’s my implementation:

(defmacro multiple-value-constantly (&rest forms)         
    (lambda (&rest l) (lambda (&rest any)                                       
                        (declare (ignore any))                                  
                        (values-list l)))                                       

May induce funny feeling in Lisp programmers:

(setf (symbol-function 'constantly-nothing)


Of course, you have to be careful using multiple values; they can be tricky.

Secrets of Reducing Your Carbon Footprint


Can we grow willow and bury it?

This carbon footprint article from The Independent reckons that the average Briton’s carbon footprint is 10.92 tons of CO2.

This article about phytoremediation in Sweden suggests short rotatation coppicing gives a yield of about 6-12 tonnes of oven-dried willow per hectare per year. Similar yields in England are suggested by the survey results that I got Ian Tubby of the Forestry Commission’s Biomass Energy Centre to e-mail me. Optomistic rule of thumb: 10 tonnes of willow per hectare per year.

Willow is about 50% carbon. So from one hectare we can sequester 3-6 tons of carbon.

That seems like a long way off the 10.92 tons of CO2 that we’re each responsible for producing. But hold onto your apples and oranges there. A little bit of chemistry reveals that 1 ton of carbon is equivalent to 3.67 tons of CO2. That’s because carbon has atomic weight 12, but CO2 has molecular weight 44 (12+16+16), so every 44 tons of CO2 has only 12 tons of carbon in it.

So our 10.92 tons of CO2 per year is only 2.98 tons of carbon. Which we can easily offset with 0.6 hectares of willow or so. Of course to actually offset the carbon we need to bury the willow. In a hole in the ground. Like maybe a coal mine. (And how do we replace the P, N, and K that I’ve just buried?)

Of course as well as sequestering carbon to offset my footprint I could displace carbon. Instead of burning coal (geological carbon) I could burn willow. This leaflet from some random consultants suggests that 0.7 hectare of willow is sufficient to heat a 3 bedroom house.

Anyone know the average number of KWh per year it takes to heat a house in the UK? It’s surprisingly hard to find the answer in anything approaching an SI unit. This so obviously transient page (referenced on 2007-04-20) links to dataset ST341114 from the Office of National Statistics. That gives a 2001 figure of 1210 for space heating and 450 for water heating. Per household. The units? Why, kilograms of oil equivalent of course (haven’t these guys heard of SI?). Which Wikipedia suggests is a bit of a variable quantity; 1 kilogram of oil equivalent could be 42, 41.868, or perhaps 41.85 MJ. Really I only need a rough guide. Call it 42. That’s 69.72 GJ per jear.

Plausibility check: I burn about 1.2 tonnes of anthracite a year, plus some electricity to heat water in the summer. Anthracite has calorific value of 36 MJ kg-1 so that’s 43.2 GJ plus the electricity. So we’re in the right ball park. An even cruder check would be that 1.2 tonnes of coal is surely about the same amount of heat as 1.2 tonnes of crude. This time I’m thankful that the ONS uses silly non-SI units.

Seasoned wood has a calorific value of 16 MJ kg-1. The average household will need 69.72/16 = 4358 Kg of seasoned willow. About 0.5 hectare then (and a 5-year lead time (3 for growing up to the first harvest, 2 for seasoning), eek!). So the random consultants are in the same ball park with 0.7 hectare. There are 2.4 people per household so we only need 0.2 or 0.3 of a hectare per person to displace the carbon we were using for heating (which, according the The Independent article, is .40 tonnes). The lower calorific value of willow (compared to anthracite or crude) means we need to grow more willow, but we end up burying less.

So by swallowing a few assumptions, I personally could make myself carbon neutral with less than a hectare of willow. Unfortunately the UK has only 24 million hectares of land (and no, we’re not going to be growing willow on all of it). What are the rest of you going to do?

Still, it was a nice thought experiment.

Behold the colour purple


I walked into my garden today (actually yesterday; there’s a sort of timewarp blog effect) to find it had been invaded by a load of coloured… things.

On my wall:

A thing on my wall.



Several things lurking under the table:

Small purple flowers.

A penta-thing:

Blue flower with 5-fold symmetry.



Very ominous looking things. Are they spawning vessels for more things?

Some things.

Awesome Shell Power: 2>&1 | tee …


Collect the stdout and stderr of a command into a single file and see the output interactively:

some command 2>&1 | tee err

I use this with make so often that I have a script called mk:

make "$@" 2>&1 | tee err

(and don’t forget, we always use "$@", never $*)

The output file is named err but would probably be better named ofile (suggested by Nick B in comments). I use err in deference to 15 years of my personal Unix tradition.

The Order of Redirections

Now let’s take pipe out of the equation for a moment and consider redirections alone. On my Mac OS X laptop the command ls -d /eta /etc produces the following output:

ls: /eta: No such file or directory

The “No such file or directory” message appears on stderr and “/etc” appears on stdout.

You can prove that to yourself by closing one or the other file descriptor. Play around with ls -d /eta /etc >&-, and ls -d /eta /etc 2>&- and convince yourself that this is so.

Shell redirections are processed from left to right; we can get the stderr of a command to appear on its stdout and get rid of its stdout entirely:

$ ls -d /eta /etc 2>&1 1>&-
ls: /eta: No such file or directory

You might like to think about why this works. fd 2 is redirected to fd 1, meaning output to fd 2 goes to the same place as fd 1 at the time that the redirection was made. Then fd 1 is closed; this doesn’t affect the previous redirection of fd 2.

Doing it the other way round, we get this:

$ ls -d /eta /etc 1>&- 2>&1
-bash: 1: Bad file descriptor

That’s because by the time that it comes to redirect fd 2 to fd 1, fd 1 has been closed and can’t be used for a redirect.

How can we tell that 2>&1 1>&- sends what normally appears on stderr to stdout? Does this convince you:

$ ( ls -d /eta /etc 2>&1 1>&- ) | sed 's/^/*** /'
*** ls: /eta: No such file or directory

By borrowing an extra fd you can even swap stderr and stdout over:

$ ls -d /eta /etc 3>&2 2>&1 1>&3 3>&- | sed 's/^/*** /'
*** ls: /eta: No such file or directory

Notice that the error message from ls has gone through the pipe and been transformed by sed, so it must have been sent to stdout. The normal output of ls, “/etc”, has not gone through the pipe, so must’ve been sent to stderr.

RFID tags in bullets


It might not help much, but it would be one more element in the forensic trail. Also I’d be curious to see how many bullets bought by governments actually ended up in the chests of innocent people.

Whilst I’m briefly on this topic, why don’t we record the characteristic striae when a gun is manufactured? (probably because then the database would be too large).

Awesome Shell Power: */.


I probably know lots of tips and tricks about using /bin/sh that are not widely enough known about.

Today’s shell tip: */.

It’s a shell pattern that expands to all the sub-directories of the current directory.

If you want to list all the names of the sub-directories in the current directory it’s a lot easier to type echo */. than it is to type find . -name . -o -type d -print -prune which is almost equivalent.

ls -ld */.


Get every new post delivered to your Inbox.