评估名单弱头正常形式 [英] Evaluating List to Weak Head Normal Form

查看:85
本文介绍了评估名单弱头正常形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下1到100的列表:

 >让x = [1..100] 

我运行 sprint x 观察其未评估的价值。

 > :sprint x 
x = _

然后,我运行 seq 评估它为弱头正常形式:

 > seq x()
()

但是重新运行 sprint

 显示(我的想法)是相同的值。 > :sprint x 
x = _

这是为什么?


>什么是x的类型?如果它是(Num a,Enum a)=> [a] 那么这将无法按预期工作。试试 let x = [1..100] :: [Int ]。实际上,当您用更普通的类型打印x时,GHCi专门将其转换为整数来进行打印。这意味着您看到的打印值实际上并没有存储在x的thunk中。使用具体类型避免了这个问题。


在David Young的补充说明中,GHCi版本之前不会发生此问题7.8,当启用单态限制时。


Given the following list from 1 to 100:

> let x = [1..100]

I run sprint x to observe its unevaluated value.

> :sprint x
x = _

Then, I ran seq to evaluate it to Weak Head Normal Form:

> seq x ()
()

But re-running sprint x shows (what I think) is the same value.

> :sprint x
x = _

Why is that?

解决方案

I think this bheklilr's comment should be marked as the answer:

What is the type of x? If it's (Num a, Enum a) => [a] then this won't work as expected. Try let x = [1..100] :: [Int]. In reality, when you print x with the more general type GHCi specializes it to Integer to do the printing. This means that the values you see printed are not actually stored back in x's thunk. Using a concrete type avoids this problem.

With the additional note from David Young that this problem won't occur on GHCi versions earlier than 7.8, when the monomorphism restriction was enabled.

这篇关于评估名单弱头正常形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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