Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluating List to Weak Head Normal Form

Tags:

list

haskell

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?

like image 461
Kevin Meredith Avatar asked Jun 17 '15 18:06

Kevin Meredith


1 Answers

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.

like image 175
hao Avatar answered Oct 08 '22 09:10

hao