Macros, clojure vs common lisp

前端 未结 5 1966
无人及你
无人及你 2021-02-02 13:58

A few of my friends and I are working on a new platform and we want to build it in lisp. The main attraction are macros. We all use Common Lisp but I want to explore the option

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-02 14:27

    Regarding ordinary macros here are the differences between Lisp's and Clojure's variants:

    1. Clojure is Lisp-1, while CL is Lisp-2. The history has shown, that macros in one-namespace language are generally more error-prone, because there's a bigger name collision chance. To mitigate this problem Clojure uses a non-traditional quasi-quote implementation.
    2. But it comes at a cost: as in Clojure symbols inside backquote are eagerly resolved in the namespace, where macros are defined, there are a lot of subtle issues, like this one.
    3. Clojure macros use auto-gensyms. This is advertised as an advantage, and it indeed removes some boilerplate (surely, you can achieve the same in Lisp - see defmacro!). And the conceptual question of when to use gensyms, obviously, remains.

    So, overall, Lisp's approach is more rough, but more flexible. Clojure's macros may be slightly easier to approach, but become harder to use, when you go beyond simple syntax modifications and start to define complete DSLs.

    Regarding reader macros, as you know, Clojure doesn't give this option to the user, while CL does. So in CL reader macros find a lot of uses, like string interpolation, internationalization support, SQL DSLs or java interop. Not to mention a plethora of special cases, when reader macros can be used to help create advanced DSLs.

提交回复
热议问题