免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: izhier
打印 上一主题 下一主题

怎么才叫 lazy 呢? [复制链接]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
11 [报告]
发表于 2009-03-31 22:05 |只看该作者
原帖由 win_hate 于 2009-3-31 22:03 发表

我以前没贴过啊?
Exercise 1.5.  Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is using applicative-order evaluation or normal-order evaluation. He defines the following two procedures:

(define (p) (p))

(define (test x y)
  (if (= x 0)
      0
      y))

Then he evaluates the expression

(test 0 (p))

What behavior will Ben observe with an interpreter that uses applicative-order evaluation? What behavior will he observe with an interpreter that uses normal-order evaluation? Explain your answer. (Assume that the evaluation rule for the special form if is the same whether the interpreter is using normal or applicative order: The predicate expression is evaluated first, and the result determines whether to evaluate the consequent or the alternative expression.)

论坛徽章:
0
12 [报告]
发表于 2009-03-31 22:06 |只看该作者
原帖由 flw 于 2009-3-31 22:00 发表
哦?
我换成 2 `div` 0 之后确实是可以的。
难道 Haskell 居然是 normal order 的?


拜托把http://en.wikibooks.org/wiki/Haskell/Lazinesshttp://en.wikipedia.org/wiki/Eva ... n-strict_evaluation 看完再来误导新手。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
13 [报告]
发表于 2009-03-31 22:10 |只看该作者
原帖由 roy_hu 于 2009-3-31 22:06 发表


拜托把http://en.wikibooks.org/wiki/Haskell/Lazinesshttp://en.wikipedia.org/wiki/Eva ... n-strict_evaluation 看完再来误导新手。

嗯,这两个页面确实很不错。

论坛徽章:
0
14 [报告]
发表于 2009-03-31 22:30 |只看该作者
一些纯函式语言是使用 normal 序的,但 normal 序效率有时候很低,这是早期函式语言面对的一个困难。后来 David Turner 提出了图规约,可以使同名的表达式只求值一次。Haskell 应该也是采用这种技术的。

论坛徽章:
0
15 [报告]
发表于 2009-03-31 23:17 |只看该作者
原帖由 win_hate 于 2009-3-31 21:59 发表

g=g

f b x y = if b then x else y


执行 g 会陷入无限循环。

f True 1 g 得到 1。

f False 1 g 无限循环。

这个并不能证明 lazy 的特性吧?

下面这个程序和这个差不多吧?
python:

  1. >>> def g():
  2. ...     g()
  3. ...
  4. >>> def f(cond, a, b):
  5. ...     if cond:
  6. ...             return a
  7. ...     else:
  8. ...             b()
  9. ...
  10. >>> f(True, 5, g)
  11. 5
  12. >>> f(False, 5, g)
  13. Traceback (most recent call last):
  14.   File "<stdin>", line 1, in <module>
  15.   File "<stdin>", line 5, in f
  16.   File "<stdin>", line 2, in g
  17. ...
  18. ...
  19. ...
  20. RuntimeError: maximum recursion depth exceeded
复制代码

[ 本帖最后由 izhier 于 2009-3-31 23:20 编辑 ]

论坛徽章:
0
16 [报告]
发表于 2009-03-31 23:35 |只看该作者

回复 #15 izhier 的帖子

python 里对 g 求值是个函数对象吧?

在 haskell 里, g 是个无限递归的表达式。 只要 g 一下就溢出了。

论坛徽章:
0
17 [报告]
发表于 2009-04-01 09:05 |只看该作者
原帖由 win_hate 于 2009-3-31 23:35 发表
在 haskell 里, g 是个无限递归的表达式。 只要 g 一下就溢出了。

原来 g 是个表达式!

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
18 [报告]
发表于 2009-04-01 09:50 |只看该作者
原帖由 win_hate 于 2009-3-31 22:30 发表
一些纯函式语言是使用 normal 序的,但 normal 序效率有时候很低,这是早期函式语言面对的一个困难。后来 David Turner 提出了图规约,可以使同名的表达式只求值一次。Haskell 应该也是采用这种技术的。

嗯。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
19 [报告]
发表于 2009-04-01 11:06 |只看该作者
原帖由 win_hate 于 2009-3-31 23:35 发表
python 里对 g 求值是个函数对象吧?

在 haskell 里, g 是个无限递归的表达式。 只要 g 一下就溢出了。

嗯,Python 里面等效的写法是 g()。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP