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

查看:20
本文介绍了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.

谁能举几个例子?

推荐答案

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

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.

更短的代码:您需要 20 个看起来几乎相同的类声明 - 只有有限数量的地方不同.编写一个宏形式,将差异作为参数并为您生成源代码.以后想改吗?在一处更改宏.

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天全站免登陆