博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转] Lazy evaluation
阅读量:5870 次
发布时间:2019-06-19

本文共 1887 字,大约阅读时间需要 6 分钟。

转载自: 

In , lazy evaluation or call-by-need is an  which delays the evaluation of an expression until the value of this is actually required () and which also avoids repeated evaluations (). The sharing can reduce the running time of certain functions by an exponential factor over other non-strict evaluation strategies, such as .[]

The benefits of lazy evaluation include:

  • Performance increases due to avoiding unnecessary calculations and avoiding error conditions in the evaluation of compound expressions.
  • The capability of constructing potentially infinite 
  • The capability of defining  as abstractions instead of as primitives.

In most eager languages, if statements evaluate in a lazy fashion.

if a then b else c

evaluates (a), then if and only if (a) evaluates to true does it evaluate (b), otherwise it evaluates (c). That is, either (b) or (c) will not be evaluated. Conversely, in an eager language the expected behavior is that

define f(x,y) = 2*xset k = f(e,5)

will still evaluate (e) and (f) when computing (k). However, user-defined control structures depend on exact syntax, so for example

define g(a,b,c) = if a then b else cl = g(h,i,j)

(i) and (j) would both be evaluated in an eager language. While in

l' = if h then i else j

(i) or (j) would be evaluated, but never both.

Laziness in eager languages

Python: In Python 2.x the range() function computes a list of integers (eager or immediate evaluation):

>>> r = range(10) >>> print r [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> print r[3] 3

In Python 3.x the range() function returns an iterator which computes elements of the list on demand (lazy or deferred evaluation):

>>> r = range(10) >>> print(r) range(0, 10) >>> print(r[3]) 3
This change to lazy evaluation saves execution time for large ranges which may never be fully referenced and memory usage for large ranges where only one or a few elements are needed at any time.
你可能感兴趣的文章
【nginx】配置Nginx实现负载均衡
查看>>
20160222.CCPP体系具体解释(0032天)
查看>>
Java面试必问,ThreadLocal终极篇
查看>>
Arduino
查看>>
hostPath Volume - 每天5分钟玩转 Docker 容器技术(148)
查看>>
php数组时按值传递还是按地址传递
查看>>
js向一个数组中插入元素的几个方法-性能比较
查看>>
Tensorflow代码解析(一)
查看>>
容器相关源加速以及k8s官方资源镜像下载
查看>>
Zombie进程
查看>>
asp.netmvc 三层搭建一个完整的项目
查看>>
sql server 生成随机数 rand函数
查看>>
CSS魔法堂:Transition就这么好玩
查看>>
【OpenStack】network相关知识学习
查看>>
centos 7下独立的python 2.7环境安装
查看>>
[日常] 算法-单链表的创建
查看>>
用ASP.NET Core 2.1 建立规范的 REST API -- 保护API和其它
查看>>
前端工程化系列[01]-Bower包管理工具的使用
查看>>
使用 maven 自动将源码打包并发布
查看>>
Redis 的源码分析
查看>>