What advantage does common lisp reader macros have that Clojure does not have?

*爱你&永不变心* 提交于 2020-01-29 02:51:05

问题


I have been using Clojure alot recently but I still don't understand what functionality I do not get that common lisp reader macros provide. Can explain explain this to me in simple terms?


回答1:


In short, reader macros provide you with the ability to redefine the programming language's syntax within some delimited context.

For example, you could implement regular expression literals (e.g. #"pattern") yourself given reader macros. Without them, you would be forced to properly escape regular expressions in string literals passed to re-pattern.

BTW, while there are no public Clojure APIs for modifying the reader, it's not impossible, as illustrated in these posts:

  • http://briancarper.net/blog/449/clojure-reader-macros
  • http://fulldisclojure.blogspot.com/2009/12/how-to-write-clojure-reader-macro.html



回答2:


A simple example. Common Lisp has a different reader syntax for vectors #() instead of []. But with the ability to create custom reader macros you can have a reader macro that traslates [2 3 4 5] to a vector in Common Lisp as well.

Since most users won't be aware of the meaning of reader macros one has created they are rarely used and to avoid the confusion altogether Rich Hickey decided to remove the ability to have user defined reader macros in Clojure. Clojure, however, has predefined reader macros - quote, vector, regex, map, etc




回答3:


In Common Lisp the reader is user extensible with reader macros. The reader is responsible to read s-expressions. S-expressions are an external textual syntax for Lisp's data types like numbers, strings, symbols, lists, conses, structures, arrays, characters, ...

The reader is not responsible for the syntax of the programming language Lisp - just for s-expressions.

Thus the main purpose, from the point of the user, for reader macros is to extend or change the syntax of s-expressions. For example the user can add textual syntax for various CLOS classes (like URLs, ...), hash-tables, special identifiers, new number types, ...

Sometimes it is also used to embed syntax of other languages/syntax, which have different rules to form tokens: embedded SQL, embedded C, infix expressions, embedded calls to Objective C, embedded rule languages, embedded XML, embedded JSON and more.

Another use is to allow the user to have additional control over the s-expressions, the reader actually reads. For example conditional feature expressions.

Thus user programmable reader macros allow the user to customize the reader with regard to the above described functionality. One can imagine that this is useful for those users, who want to customize the language at a data-syntax/token level, but it adds another layer of complexity.



来源:https://stackoverflow.com/questions/5746801/what-advantage-does-common-lisp-reader-macros-have-that-clojure-does-not-have

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!