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?
相关问题
- Better Sequence Duplicate Remover
- Drakma and Dexador both fails at USocket call whil
- Installation of Leiningen 2.X in Mac OS X
- Questions about Lists and other stuff in Clojure
- How do I add CORS to a compojure-api app?
相关文章
- Factor Clojure code setting many different fields
- Does learning one Lisp help in learning the other?
- Common Lisp: Why does my tail-recursive function c
- How do I write a macro-defining macro in common li
- How can I unintern a qualified method?
- Better way to nest if-let in clojure
- Idiomatic approach for structuring Clojure source
- Changing the nth element of a list
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
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 tore-pattern
.BTW, while there are no public Clojure APIs for modifying the reader, it's not impossible, as illustrated in these posts:
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.