Some C programs are just LISP in disguise

After learning LISP I’m increasingly noticing open-coded emulations of LISP facilities. While every LISP advocate talks about macros, macros are not the only reason why LISP is worth learning.

Dynamic variables look like an obscure feature inherited from the times of LISP interpreters, but there is no really clean way to emulate the functionality if the language doesn’t provide it. (On the other hand, the comprehensive LISP condition system can be implemented as a set of macros on top of dynamic variables.)

This is the current code in C. The cleanup handler is invoked also when an error occurs, before longjmp ().

static void output_raw_cleanup(void *xold)
{
  int *old = xold;
  output_raw = *old;
}

  int original_output_raw = output_raw;
  output_raw = 1;
  cleanup_push(&original_output_raw, output_raw_cleanup);
  do_some_output();
  cleanup_pop();

The equivalent in Java would look like this:

int originalOutputRaw = Output.outputRaw;
Output.outputRaw = true;
try {
    do_some_output();
} finally {
    output.outputRaw = originalOutputRaw;
}

In LISP:

(let ((*output-raw* t))
  (do-some-output))

Yet there are sound technical reasons why the software has to be written in C. Can I be an LISP elitist without actually writing software in LISP? ;-)

Leave a Reply