Why is tcl broken?

Gareth McCaughan Gareth.McCaughan at pobox.com
Thu Jul 1 12:51:28 EDT 1999


Paul Duffin wrote:

> I was referring to the fact that CL macros 'complicate' the parsing /
> evaluation rules of Lisp which otherwise would be very simple and
> consistent.
> 
> I personally like Lisp a lot and take advantage of lots of short cuts
> that macros provide but I think that there is a big learning curve to
> climb if you want to write one yourself (which is why I referred to it
> as another language). Tcl on the other hand allows me to use the same
> commands that I use to write normal programs to extend the language by
> adding new programming constructs.

So does Lisp. If you want (foobar x y) to be the same as (setq y x)
you say

    (defmacro foobar (x y)
      `(setq ,y ,x))

which is hardly terribly hairy. If you want (n-times n (wibble spong))
to be the same as n repetitions of (wibble spong) you say

    (defmacro n-times (n form)
      `(progn . ,(loop repeat n collect form)))

If you want (with-foo (var arg1 arg2) blah blah blah) to be the same
as (let ((var (foo arg1 arg2))) blah), you say

    (defmacro with-foo ((var . args) &body forms)
      `(let ((,var (foo . ,args))) . ,forms))

This is all the same Lisp language as you write functions in.
The backquote form is most commonly used when writing macros,
but it's useful for other things too. For instance, I hacked up
a symbolic differentiation program recently, and it's full of
things like `(+ (* ,(d f x) ,g) (* ,f ,(d g x))). Writing
macros is certainly a slightly different *skill* from writing
ordinary code, but it's not remotely a different *language*.

Incidentally, how would you implement equivalents of those
things in Tcl?

Of course a macro isn't the *same* as a function. But then,
if you want something the same as a function, you can use a
function :-). Perhaps what you want is a function that doesn't
evaluate its arguments. Some old versions of Lisp had things
called FEXPRs, which were exactly that. But, after experience
of using them, it turned out that they weren't the Right Thing,
and something much more like the present macro system was born.
Kent Pitman (who posts regularly here) wrote an article about
why FEXPRs aren't good enough, back around the time when the
transition to macros happened. It's available on the web
somewhere.

If you don't like having to construct code explicitly (though
I find the backquote syntax makes this a non-issue), you might
prefer the Scheme macro system. It's rather interesting.

-- 
Gareth McCaughan            Dept. of Pure Mathematics & Math. Statistics,
Gareth.McCaughan at pobox.com  Cambridge University, England.




More information about the Python-list mailing list