Extending a Language – Writing Powerful Macros in Scheme

https://news.ycombinator.com/rss Hits: 5
Summary

The Lisp languages, and thus Scheme as well, are homoiconic programming languages, which means that if the program's internal representation is a datum of the language. In first approximation, the internal representation of a Scheme expression (as of a Scheme program) is a Scheme datum value. For example, the program (expression) is represented by a list whose first element is the symbol let, whose second element is a list of a list with two elements and whose third element is a list of the three data +, x, and 2. Due to existence of hygienic macros we have to amend this traditional picture. Consider the following example. (let ([set! 10]) (incr! set!) set!) To evaluate the let expression, the macro use of incr! has to be expanded first. After the expansion, the expression would look like (let ([set! 10]) (set! set! (+ set! 1)) set!) if Scheme expressions were represented by Scheme datum values and within, identifiers were represented by symbols. It is obvious that this cannot be how the Scheme expander works because there would be no way to tell which copy of the symbol set! refers to which binding. The point is that identifiers cannot be represented by symbols, which only have a symbolic name. Instead, to an identifier both a symbolic name and a lexical context are associated. When the binding of an identifier is looked up, it is looked up in the lexical context associated with it. In Scheme, symbols are first-class values. The can be created using the syntax (quote name), which can be abbreviated to 'name: red The same is true for identifiers. They are created just like symbols but use the syntax (syntax identifier), which can be abbreviated to #'identifier, instead: #<syntax x> (The format of the output, #<syntax x>, is implementation-specific, because identifiers are not Scheme datum values and thus have no standardized or faithful written representation.) Evaluating of the form (syntax x) (or #'x) means the following for the Scheme system: construct and return...

First seen: 2025-05-08 09:20

Last seen: 2025-05-08 13:42