惰性求值与宏 [英] Lazy Evaluation vs Macros

查看:20
本文介绍了惰性求值与宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经习惯了 Haskell 的惰性求值,现在我已经正确地使用了惰性求值,我发现自己对默认急切的语言感到恼火.这实际上是相当有害的,因为我使用的其他语言主要使懒惰地评估东西变得非常尴尬,通常涉及推出自定义迭代器等等.因此,仅仅通过获得一些知识,我实际上已经降低了在我的原始语言方面的工作效率.叹息.

但我听说 AST 宏提供了另一种干净的方式来做同样的事情.我经常听到诸如懒惰评估使宏变得多余"之类的说法,反之亦然,主要来自 Lisp 和 Haskell 社区.

我涉足过各种 Lisp 变体中的宏.它们似乎是一种非常有组织的复制和粘贴代码块的方式,以便在编译时处理.他们当然不是 Lispers 认为的圣杯.但这几乎可以肯定是因为我无法正确使用它们.当然,让宏系统在与语言本身组装的相同核心数据结构上工作确实很有用,但它基本上仍然是一种有组织的复制和粘贴代码的方式.我承认,将宏系统基于与允许完全运行时更改的语言相同的 AST 非常强大.

我想知道的是,如何使用宏来简明扼要地完成惰性求值的工作?如果我想逐行处理文件而不破坏整个文件,我只需返回一个列表,其中映射了一个行读取例程.这是 DWIM 的完美示例(按照我的意思去做).我什至不必考虑.

我显然没有得到宏.我已经使用过它们,但考虑到炒作并没有给我留下特别深刻的印象.因此,我缺少一些我无法通过在线阅读文档来获得的东西.有人可以向我解释这一切吗?

解决方案

惰性求值可以替代宏的某些用途(那些延迟求值以创建控制结构的宏),但反之则不然.您可以使用宏使延迟评估结构更加透明——请参阅 SRFI 41 (Streams) 以获取有关如何操作的示例:http://download.plt-scheme.org/doc/4.1.5/html/srfi-std/srfi-41/srfi-41.html

除此之外,您还可以编写自己的惰性 IO 原语.

然而,根据我的经验,与设计为从一开始就有效支持它的运行时中的普遍惰性代码相比,严格语言中的普遍惰性代码往往会引入开销 - 请注意,这确实是一个实现问题.

I'm used to lazy evaluation from Haskell, and find myself getting irritated with eager-by-default languages now that I've used lazy evaluation properly. This is actually quite damaging, as the other languages I use mainly make lazily evaluating stuff very awkward, normally involving the rolling out of custom iterators and so forth. So just by acquiring some knowledge, I've actually made myself less productive in my original languages. Sigh.

But I hear that AST macros offer another clean way of doing the same thing. I've often heard statements like 'Lazy evaluation makes macros redundant' and vice-versa, mostly from sparring Lisp and Haskell communities.

I've dabbled with macros in various Lisp variants. They just seemed like a really organized way of copy and pasting chunks of code around to be handled at compile time. They certainly weren't the holy grail that Lispers like to think it is. But that's almost certainly because I can't use them properly. Of course, having the macro system work on the same core data structure that the language itself is assembled with is really useful, but it's still basically an organized way of copy-and-pasting code around. I acknowledge that basing a macro system on the same AST as the language that allows full runtime alteration is powerful.

What I want to know is, is how can macros be used to concisely and succinctly do what lazy-evaluation does? If I want to process a file line by line without slurping up the whole thing, I just return a list that's had a line-reading routine mapped over it. It's the perfect example of DWIM (do what I mean). I don't even have to think about it.

I clearly don't get macros. I've used them and not been particularly impressed given the hype. So there's something I'm missing that I'm not getting by reading over documentation online. Can someone explain all of this to me?

解决方案

Lazy evaluation can substitute for certain uses of macros (those which delay evaluation to create control constructs) but the converse isn't really true. You can use macros to make delayed evaluation constructs more transparent -- see SRFI 41 (Streams) for an example of how: http://download.plt-scheme.org/doc/4.1.5/html/srfi-std/srfi-41/srfi-41.html

On top of this, you could write your own lazy IO primitives as well.

In my experience, however, pervasively lazy code in a strict language tends to introduce an overhead as compared to pervasively lazy code in a runtime designed to efficiently support it from the start -- which, mind you, is an implementation issue really.

这篇关于惰性求值与宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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