F# - 需要帮助某人解释懒惰函数以及为什么我得到某个结果

时间:2010-07-03 04:57:54

标签: f#

我有一个问题,为什么我在F#中获得某些结果。我有以下代码......

let lHelloWorld = lazy(printfn "Lazy Hello World"; 30+30)
let aHelloWorld = (printfn "Active Hello World"; 30+30)

printfn "lazy value is %d"  lHelloWorld.Value 
printfn "lazy value is %d"  lHelloWorld.Value
printfn "active value is %d"  aHelloWorld
printfn "active value is %d"  aHelloWorld

我的输出如下......

Active Hello World
Lazy Hello World
lazy value is 60
lazy value is 60
active value is 60
active value is 60

我无法理解的是......为什么在懒惰的hello字之前显示活动的hello world的printfn?我原本期望“懒人世界”在“活跃的Hello World”之前出现过吗?

如果有人可以帮忙解释一下,我们将不胜感激。

2 个答案:

答案 0 :(得分:6)

这是我的注释说明

// Here we go...
let lHelloWorld = lazy(printfn "Lazy Hello World"; 30+30) 
// Ok, we defined a value called lHelloWorld.  The right hand side is 
// a lazy(...), so we don't evaluate the ..., we just store it in a
// System.Lazy object and assign that value to lHelloWorld.

let aHelloWorld = (printfn "Active Hello World"; 30+30) 
// Ok, we're defining a value called aHelloWorld.  The right hand side is
// a normal expression, so we evaluate it it right now.  Which means we
// print "Active Hello World", and then compute 30+30=60, and assign the
// final result (60) to aHelloWorld.

// Note that neither of the previous two entities are functions.


// Now we have some effects:
printfn "lazy value is %d"  lHelloWorld.Value  
// Calling .Value on an object of type System.Lazy will either
//  - force the value if it has not yet been evaluated, or
//  - return the cached value if it was previously evaluated
// Here we have not yet evaluated it, so we force the evaluation, 
// which prints "Lazy Hello World" and then computes 30+30=60, and then
// stores the final 60 value in the lHelloWorld's cache.  Having evaluated 
// the arguments to the printfn on this line of code, we can now call that
// printfn, which prints "lazy value is 60".

printfn "lazy value is %d"  lHelloWorld.Value 
// This time, calling .Value just fetches the already-computed cached value,
// 60, and so this just prints "lazy value is 60".

printfn "active value is %d"  aHelloWorld 
// aHelloWorld has type 'int'.  Its value is 60.  This is trivial, 
// it prints "active value is 60".
printfn "active value is %d"  aHelloWorld 
// Ditto.

答案 1 :(得分:1)

嗯,我不是一个很大的F#人,但是它设置得很懒,所以在他们提出请求之前他们什么都不做。所以当你把它包装成懒惰时,它会在你实际使用它之后才会运行。但是,由于您没有将它用于活动的,因此只要它被分配就会执行该功能。

这只是猜测,因为我不是F#家伙。

这篇文章也解释了这一点。 http://weblogs.asp.net/podwysocki/archive/2008/03/21/adventures-in-f-f-101-part-6-lazy-evaluation.aspx

抱歉忘了附上链接。