阅读宏:您将它们用于什么? [英] Read macros: what do you use them for?

查看:97
本文介绍了阅读宏:您将它们用于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解一下到目前为止我还没有使用过的Lisp部件.目前,读取宏引起了我的注意.关于其使用的信息并不多,这将有助于了解人们对它们的处理方式,既可以获取有关其工作方式的示例,也可以了解可能出现的问题类型.与他们接近.在此之后,是否有任何指导方针来了解什么构成读取宏的好坏?

I'm trying to get a feel for the parts of Lisp that I haven't used very much up to now. Read macros have caught my attention at the moment. There isn't a huge amount of info about their use and it would help to see what people have done with them, both to get examples of how they work and also to see what sorts of problems can be approached with them. Following on that, are there any guidelines for knowing what constitutes good and bad use of read macros?

推荐答案

S表达式是Lisp对Lisp数据的语法. S表达式使用READ函数读取,而读取宏是Lisp扩展读取器的内置方法.这意味着读取宏的最直接用途是实现预定义的数据语法,并为更改或扩展Lisp读取s表达式的方式提供可能性.

S-expressions are Lisp's syntax for Lisp data. S-expressions are read with the function READ and read macros are Lisp's built-in way to extend the reader. This means that the most direct use of read macros is to implement the pre-defined data syntax and open up possibilities to change or extend the way Lisp reads s-expressions.

Lisp带有针对许多数据类型的预定义外部语法:符号,数字,字符串,数组,字符,conses,列表,结构等.它允许打印和读取数据对象.

Lisp comes with a pre-defined external syntax for a lot of data types: symbols, numbers, strings, arrays, characters, conses, lists, structures and more. It allows data objects to be printed and read back.

  1. Lisp缺少其他几种数据类型的语法-显着的哈希表和CLOS对象.因此,用户代码中读取宏的首次使用将是扩展读取器,使其能够读取哈希表,并行向量,新数字类型等数据结构,...基本上,每种数据类型开发人员都希望拥有一种外部语法,可以读回.

  1. Lisp lacks syntax for several other data types - prominently hash tables and CLOS objects. So the first use of read macros in user code would be to extend the reader to be able to read data structures like hash tables, parallel vectors, new number types, ... basically every data type the developer wants to have an external syntax that can be read back.

由于Lisp还将s表达式用于代码,因此读取宏的第二个用途是扩展Lisp程序的表示法.一个典型的例子是使用[和]编写嵌入式SQL代码.常用的Lisp语法看起来很相似,但是[和]的使用有助于SQL表达式在代码中脱颖而出.另一个示例是使用读取宏为嵌入式编程语言提供标识符,例如Objective C常量,消息等.Clozure CL使用此宏来表示区分大小写/区分大小写的标识符,并在读取时使用索引来查找它们的定义.外部可用的标识符.

Since Lisp uses s-expressions also for code, the second use of read macros is to extend the notation for Lisp programs. A typical example is the use of [ and ] to write embedded SQL code. The usual Lisp syntax looks similar, but the use of [ and] helps the SQL expressions to stand out in the code. Another example is to use read macros to provide identifiers for embedded programming languages, like Objective C constants, messages, etc.. Clozure CL uses this to represent case sensitive / case preserving identifiers and to look up their definition during read time using an index of externally available identifiers.

第三个用途是将不同的语法嵌入Lisp语法中.一个老的例子是infix read宏,它允许嵌入infix表达式.其他示例是嵌入式HTML或XML语法,或其他编程语言语法的嵌入式片段.

The third use is to embed different syntaxes into Lisp syntax. An old example for that is the infix read macro, which allows embedded infix expressions. Other examples are embedded HTML or XML syntax, or embedded fragments of other programming language syntaxes.

有时,读取宏用于实现使用与预定义Common Lisp语法不同的s-表达式语法的其他(相关)语言.例如,Scheme s表达式的读者-与Common Lisp稍有不同.

Sometimes read macros are used to implement other (related) languages that use s-expression syntaxes that are different from the pre-defined Common Lisp syntax. An example would be a reader for Scheme s-expressions - which are slightly different from Common Lisp.

这篇关于阅读宏:您将它们用于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆