为什么让y = 1 + y编译,这是什么意思? [英] Why does let y = 1 + y compile, and what does it mean?

查看:167
本文介绍了为什么让y = 1 + y编译,这是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GHCi let y = y + 1 编译得很好,但当我尝试评估y时,我得到了 ***异常:<<<为什么没有编译错误,它是什么意思<< loop>>< / code>>< >

解决方案

Haskell let 其中和顶级绑定默认为递归,即使它们不适用于函数。所以 let y = y + 1 定义了一个将 1 添加到数字的无限循环。 GHC用<< loop>>< / code>例外来表示这样的循环 - 当然,如果它能够捕捉到它们的话!



这适用于惰性操作,因为它允许我们轻松定义像无限列表( let xs = 0:xs )的东西,对于普通代码实际上很有用但是,它不能用于 + (对于大多数数字类型)的严格操作,因为它们需要立即评估整个(无限)事物。


In GHCi let y = y + 1 compiles fine, but when I try to evaluate y I got *** Exception: <<loop>>

Why is there no compile error and what does it mean <<loop>>?

解决方案

Haskell let, where and top-level bindings are recursive by default, even if they're not for a function. So let y = y + 1 defines an infinite loop of adding 1 to a number. GHC represents loops like this with the <<loop>> exception—if it can catch them, of course!

This is usable for lazy operations, because it allows us to easily define things like infinite lists (let xs = 0:xs), which are well-defined and actually useful for normal code. However, it can't work for strict operations like + (for most numeric types) because they require evaluating the whole (infinite) thing right away.

这篇关于为什么让y = 1 + y编译,这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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