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?
Thus read-macros are at least as powerful as ordinary macros. (Note that read-macros and reader macros mean the same thing) The Lisp reader is the thing that parses and translates raw text into an AST. In Lisp, the AST is better known as an s-expression (or sexp) – its either an atom or a list.
Clojure has a programmatic macro system which allows the compiler to be extended by user code. Macros can be used to define syntactic constructs which would require primitives or built-in support in other languages. Many core constructs of Clojure are not, in fact, primitives, but are normal macros.
This reader macro translates an expression of the form '<some-form> into (quote <some-form>). Simply put, the single quote character ' is a little bit of syntactic sugar added on top of Lisp. Reader macros do just that – allow you to add syntactic sugar to Lisp.
Many core constructs of Clojure are not, in fact, primitives, but are normal macros. Some macros produce simple combinations of primitive forms. For example, when combines if and do: user=> (macroexpand ' (when (pos?
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:
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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With