user3560270
user3560270

Reputation: 443

How can seq evaluate an inifinite list in Haskell?

It is said that the Haskell seq function forces the evaluation of its first argument and returns the second. It is used to add strictness to evaluation of expressions. So how can the following simply return 5:

seq [1..] 5

Shouldn't it get stuck in trying to construct an infinite list?

Upvotes: 2

Views: 209

Answers (1)

Michael Snoyman
Michael Snoyman

Reputation: 31305

seq evaluates to weak head normal form (WHNF), which essentially means it evaluates one layer of data constructors. In this case, it means that it forces evaluation of the first cons cell (the : data constructor).

I have a pretty long post explaining details around this at https://haskell.fpcomplete.com/tutorial/all-about-strictness

Upvotes: 12

Related Questions