简体   繁体   English

评估列表到弱头范式

[英]Evaluating List to Weak Head Normal Form

Given the following list from 1 to 100: 给出以下从1到100的列表:

> let x = [1..100]

I run sprint x to observe its unevaluated value. 我运行sprint x来观察其未评估的价值。

> :sprint x
x = _

Then, I ran seq to evaluate it to Weak Head Normal Form: 然后,我运行seq将其评估为Weak Head Normal Form:

> seq x ()
()

But re-running sprint x shows ( what I think ) is the same value. 但重新运行sprint x显示( 我认为 )是相同的值。

> :sprint x
x = _

Why is that? 这是为什么?

I think this bheklilr's comment should be marked as the answer: 我认为这个bheklilr的评论应该标记为答案:

What is the type of x? x的类型是什么? If it's (Num a, Enum a) => [a] then this won't work as expected. 如果它是(Num a, Enum a) => [a]那么这将无法按预期工作。 Try let x = [1..100] :: [Int ]. 试试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. 实际上,当您使用更通用的类型打印x时,GHCi将其专门用于Integer进行打印。 This means that the values you see printed are not actually stored back in x's thunk. 这意味着您看到的打印值实际上并不存储在x的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. 使用David Young的附加说明,当启用单态限制时,在7.8之前的GHCi版本上不会出现此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM