For faster navigation, this Iframe is preloading the Wikiwand page for 惰性求值.

惰性求值

此条目没有列出任何参考或来源。 (2017年5月15日)维基百科所有的内容都应该可供查证。请协助补充可靠来源改善这篇条目。无法查证的内容可能会因为异议提出而被移除。

编程语言理论中,惰性求值(英语:Lazy Evaluation),又译为惰性计算懒惰求值,也称为传需求调用(call-by-need),是计算机编程中的一个概念,目的是要最小化计算机要做的工作。惰性计算的最重要的好处是它可以在空间复杂度上得到极大的优化,从而可以轻易构造一个无限大的数据类型

惰性求值的相反是及早求值,这是一个大多数编程语言,如C语言,所使用的缺省计算方式。

由于翻译问题,该词在不同语境下有两个相关而又有区别的含意,可以表示为“延迟求值”和“最小化求值”,本条目主要内容为延迟求值,后者请参见最小化计算条目。

延迟求值

延迟求值特别用于匿名式函数编程,在使用延迟求值的时候,表达式不在它被绑定到变量之后就立即求值,而是在该值被取用的时候求值,也就是说,语句如x:=expression; (把一个表达式的结果赋值给一个变量)明显的调用这个表达式被计算并把结果放置到x中,但是先不管实际在x中的是什么,直到通过后面的表达式中到x的引用而有了对它的值的需求的时候,而后面表达式自身的求值也可以被延迟,最终为了生成让外界看到的某个符号而计算这个快速增长的依赖树。

某些编程语言默认进行惰性求值(如MirandaHaskell),另一些语言提供函数或特殊语法来延迟求值(如Schemedelayforce)。

延迟求值的一个好处是能够建立可计算的无限列表而没有妨碍计算的无限循环或大小问题。例如,可以建立生成无限斐波那契数列表的函数(经常叫做“流”)。第n个斐波那契数的计算仅是从这个无限列表上提取出这个元素,它只要求计算这个列表的前n个成员。

例如,在Haskell中,斐波纳契数的列表可以写为

  fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

在Haskell语法中,":"向列表头部添加一个元素,tail返回去掉第一个元素的一个列表,而zipWith使用指定的函数(这里是加法)来组合两个列表的对应元素生成第三个列表。

假定编程者是仔细的,只有生成特定结果所需要的值才被求值。但是特定计算可能导致程序尝试对无限数目个元素进行求值;例如,求列表的长度或使用fold运算对列表的元素求和将导致程序不能终止或耗尽内存。

语言实现

C++

在典型的大规模计算(如矩阵运算)中,并不是所有的结果值都是必须求出的,很多结果值并没有被用到。因此,C++中实现延迟求值也是现实需求。一般把运算符函数函数重载,返回一个对象保存了计算所需的各种信息(如实参)等。例如,对矩阵加法的延迟求值:

struct matrix{
    double values[100][100]  ;
    double* operator[](int i){return values[i];}
};

struct matrix_add {
   mutable struct rowEntry{ 
       double operator [](int k){
           matrix_add* pthis=(matrix_add*)this;
           return (pthis->a)[_currentRow][k]+(pthis->b)[_currentRow][k];
        }
       void setRow(int v){_currentRow=v;}
   private:
       int _currentRow;
   } _rowEntry;
   matrix_add(matrix & a, matrix & b) : a(a), b(b) { }
   rowEntry& operator[](int i)const {
       _rowEntry.setRow(i); 
       return _rowEntry;
    }
private:
    matrix & a;
    matrix  & b;
};

matrix_add operator +(matrix & a, matrix & b) {
    return matrix_add(a, b);
}

int main ()
{
    matrix A,B;
    A[3][4]=101; B[3][4]=102;
    matrix_add R=A+B;
    int result = R[3][4];
    return 0;
}

参阅

外部链接

{{bottomLinkPreText}} {{bottomLinkText}}
惰性求值
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?