Lisp宏可用于的示例 [英] Examples of what Lisp's macros can be used for

查看:91
本文介绍了Lisp宏可用于的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说Lisp的宏系统非常强大.但是,我发现很难找到一些实际的例子来说明它们的用途.没有它们,这些事情将很难实现.

I've heard that Lisp's macro system is very powerful. However, I find it difficult to find some practical examples of what they can be used for; things that would be difficult to achieve without them.

有人可以举一些例子吗?

Can anyone give some examples?

推荐答案

源代码转换.所有种类.例子:

Source code transformations. All kinds. Examples:

  • 新的控制流语句:您需要WHILE语句吗?您的语言没有?为什么要等到仁慈的独裁者明年再增加一位.自己写.五分钟之内.

  • New control flow statements: You need a WHILE statement? Your language doesn't have one? Why wait for the benevolent dictator to maybe add one next year. Write it yourself. In five minutes.

Shorter代码:您需要二十个几乎完全相同的类声明-仅有少数地方是不同的.编写一个将差异作为参数并为您生成源代码的宏表格.想要稍后更改吗?在一处更改宏.

Shorter code: You need twenty class declarations that almost look identical - only a limited amount of places are different. Write a macro form that takes the differences as parameter and generates the source code for you. Want to change it later? Change the macro in one place.

源代码树中的替换:您想向源代码树中添加代码吗?变量真的应该是函数调用吗?将代码环绕在遍历"源代码并更改其找到变量的位置的代码周围.

Replacements in the source tree: You want to add code into the source tree? A variable really should be a function call? Wrap a macro around the code that 'walks' the source and changes the places where it finds the variable.

后缀语法:您想以后缀形式编写代码吗?使用一个宏将代码重写为普通形式(Lisp中的前缀).

Postfix syntax: You want to write your code in postfix form? Use a macro that rewrites the code to the normal form (prefix in Lisp).

编译时效果:您需要在编译器环境中运行一些代码来告知开发环境有关定义吗?宏可以生成在编译时运行的代码.

Compile-time effects: You need to run some code in the compiler environment to inform the development environment about definitions? Macros can generate code that runs at compile time.

编译时的代码简化/优化:您想在编译时简化一些代码吗?使用可以简化的宏-这样,您可以根据源表单将工作从运行时转移到编译时.

Code simplifications/optimizations at compile-time: You want to simplify some code at compile time? Use a macro that does the simplification - that way you can shift work from runtime to compile time, based on the source forms.

从描述/配置生成代码:您需要编写复杂的类组合.例如,您的窗口有一个类,子窗格有一个类,窗格之间有空间限制,您有一个命令循环,一个菜单以及许多其他内容.写一个宏,捕获窗口及其组件的描述,并从描述中创建驱动应用程序的类和命令.

Code generation from descriptions/configurations: You need to write a complex mix of classes. For example your window has a class, subpanes have classes, there are space constraints between panes, you have a command loop, a menu and a whole bunch of other things. Write a macro that captures the description of your window and its components and creates the classes and the commands that drive the application - from the description.

语法改进:有些语言语法看起来不太方便?编写一个宏,使应用程序编写者对您更方便.

Syntax improvements: Some language syntax looks not very convenient? Write a macro that makes it more convenient for you, the application writer.

域特定语言:您需要的语言与您的应用程序域更接近吗?用一堆宏创建必要的语言形式.

Domain specific languages: You need a language that is nearer to the domain of your application? Create the necessary language forms with a bunch of macros.

元语言抽象

基本思想:语言水平上的所有内容(新表格,新语法,表格转换,简化,IDE支持等)现在都可以由开发人员逐个进行编程-无需单独的宏处理阶段.

The basic idea: everything that is on the linguistic level (new forms, new syntax, form transformations, simplification, IDE support, ...) can now be programmed by the developer piece by piece - no separate macro processing stage.

这篇关于Lisp宏可用于的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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