Alan Bawden [2021-11-17 23:09:59] wrote:
> Stefan Monnier <
mon...@iro.umontreal.ca> writes:
>
> I've been toying with the idea of defining (in ELisp) `,` as a macro
> which would "naturally" generalize the `,` of the backquote macro to its
> meaning outside of backquotes:
>
> (defmacro \, (exp)
> (eval exp))
>
> Then you have that
>
> ,`EXP
>
> is equivalent to
>
> EXP
>
> at top-level as well.
>
> AFAICT this is not done in Common-Lisp, so I'm wondering if there's
> a particular reason why it is so, and whether other Lisps have adopted
> such a thing (or not and why not),
>
> Random unconnected thoughts:
>
> Your simple defmacro above will be evaluating that expression in an
> environment other than the lexical one where the expression appeared.
Depends what you mean by "the lexical one where the expression appeared".
E.g. in:
`(let ((x 2)) ,(+ x 5))
do you consider the `(x 2)` binding as part of the lexical environment
where `(+ x 5)` appears? [ I hope not. ]
> Common Lisp has "#." which calls eval at read time.
>
> Lisp Machine Lisp had "#," which was like "#." but delayed executing the
> form until load time. "#," was removed from Common Lisp because it was
> difficult for everyone to agree on what that meant exactly.
Hmm... these are quite different: the purpose of the above `,` is to be
able to build *code* at compile-time.
> Scheme allows you to write ",foo" anywhere and just reads it as
> "(unquote foo)".
Same in ELisp: if you write ,foo it'll be read as (\, foo).
The question is whether `unquote` or `,` is globally defined as
a function macro or not.
> I always thought that it was an advantage in Common Lisp that ","
> outside of a backquote was an error, because it would catch mistakes
> where I missed a backquote somewhere -- similar to checking that my
> parentheses were balanced.
Of course, that's the usual tradeoff.
BTW, one downside of the above `,` macro is that it will inevitably make
programmers want to have a matching `,@` top-level functionality, but
that can't be defined as cleanly/simply with a macro.
Stefan