懒惰评估与宏 [英] Lazy Evaluation vs Macros

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

问题描述

我习惯了从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天全站免登陆