Sunday, December 4, 2005

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? ;-)

2 comments:

  1. When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is
    added I get four emails with the same comment. Is there any way you can remove me from that service?
    Thanks a lot!

    ReplyDelete
  2. I for all time emailed this blog post page
    to all my associates, as if like to read it afterward my
    contacts will too.

    ReplyDelete