Evaluating List to Weak Head Normal Form

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.

链接地址: http://www.djcxy.com/p/43020.html

上一篇: 在Haskell中如何派生工作?

下一篇: 评估名单弱头正常形式