Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] Alternatives to sed + awk



Simon Cozens writes:

 > So long as it's a domain that happens to have exactly the same syntax as Lisp.

I can't speak to the Ruby version of the above sentence, but although
I'm sympathetic to it in the Lisp version, I'm not 100% so.

The reason is that even without reader macros, Lisp is "asymptotically
syntax-free".  There are a couple of bumps in the road, which I'll
admit up front: you can't do arbitrary things with lexical syntax (eg,
quote marks and parentheses can't be redefined), and you can't have
significant whitespace (except for separating tokens).[1]  And you'll
always need a few parentheses visible to get the ball rolling (eg,
requiring the package that implements the DSL, and around the list
that contains the program in the DSL).  However, in the DSL part you
can do almost anything you want with symbols.

The classic example of a DSL in Lisp is the loop macro, which
implements a very un-Lispy DSL for describing iteration.  Here's a
relatively extreme, but not untypical, example from the HyperSpec[2]:

;; Another example of the extended form of LOOP.
 (loop for n from 1 to 10
       when (oddp n)
         collect n)
=>  (1 3 5 7 9)

Look Ma!  No parentheses!  Almost: you have to have parens at the
beginning and end of the loop expression.  And the parity test has
parentheses.  But note!  The parity test is not part of the domain
(ie, specifying iteration)!  It would also be possible to get rid of
the parentheses for functions with constant arity, ie, you in theory
could write an alternative-loop macro which could be used like this:

 (alternative-loop for n from 1 to 10
                   when oddp n
                     collect n)

or even

 (alternative-loop for n from 1 to 10
                   when n is oddp
                     collect n)

Thus, Lisp is "asymptotically syntax-free".  If you are at all
familiar with Lisp macros, that becomes pretty obvious upon
reflection.

Then, there's also Maxima, which is a very thin lex/parse wrapper
around a DSL that lithpth.

Whaddya think, Simon?

N.B.  I don't claim this is practical.  No Lisp programmer in their
right mind would write alternative-loop, it would be too much effort
for too little gain.  It would also be fragile in practice, because
human readers would *require* proper indentation to read it.  So real
attempts at DSLs in Lisp will either display a lot of Lisp syntax, or
have a separate lex/parse stage like Maxima does.

Footnotes: 
[1]  Those are examples, not intended to be a complete list.

[2]  http://www.lispworks.com/documentation/HyperSpec/Body/m_loop.htm
     Examples are at the end.


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links