了解“评估”功能

Understanding `evaluate` Functiion

Haskelldocs解释evaluate函数:

Forces its argument to be evaluated to weak head normal form when the resultant IO action is executed.

Prelude Control.Exception> let xs = [1..100] :: [Int]                                                                   Prelude Control.Exception> :sprint xs
xs = _
Prelude Control.Exception> let ys = evaluate xs
Prelude Control.Exception> :t ys
ys :: IO [Int]
Prelude Control.Exception> ys
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,6Prelu2,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]
Prelude Control.Exception> :sprint xs
xs = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,
      24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,
      46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,
      68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,
      90,91,92,93,94,95,96,97,98,99,100]
Prelude Control.Exception> :sprint ys
ys = _

为什么 ys 不是 Weak Head Normal Form,即 :sprint ys 不等于 _ : _

您的 ys 值的类型为 IO [Int]。现在, IO 是一种抽象类型,在您的情况下可以将其视为 RealWorld -> ([Int], RealWorld) 。现在这个 IO 值已经是弱头范式了。这就是为什么当您对其执行 sprint 时,您将其视为 _ 的原因。

Why is ys not in Weak Head Normal Form, i.e. :sprint ys does not equal _ : _?

ys 外项不能是 _ : _ 因为它不是列表而是类型 IO [Int].

的值

除了 Sibi 所说的之外,还有一种方法可以看出 evaluate 实际上按照文档所说的进行操作:

GHCi> let xs = [1..100] :: [Int]
GHCi> :sprint xs
xs = _
GHCi> let a = evaluate xs >> return ()
GHCi> a
GHCi> :sprint xs
xs = 1 : _