F#-需要有人帮助解释惰性函数以及为什么我会得到某个结果
我有一个问题,为什么我会在F#中得到某些结果。我有以下代码F#-需要有人帮助解释惰性函数以及为什么我会得到某个结果,f#,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 "act
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 world的printfn显示在懒惰hello word之前?我本以为“懒惰的你好世界”会在“活跃的你好世界”之前出现
如果有人能帮我解释这一点,我将不胜感激。我不是一个喜欢偷懒的人,但我有懒惰的习惯,所以他们在被要求之前什么都不做。所以当你用lazy包装它时,它不会运行,直到你真正使用它。但是,由于您没有将其用于活动设备,因此它会在分配功能后立即执行该功能
不过,这只是一个猜测,因为我不是F#guy
这里有一篇文章也解释了这一点。
很抱歉,忘了附加链接。我不是一个大F#人,但对于懒惰模式,它被设置为在收到请求之前不做任何事情。所以当你用lazy包装它时,它不会运行,直到你真正使用它。但是,由于您没有将其用于活动设备,因此它会在分配功能后立即执行该功能
不过,这只是一个猜测,因为我不是F#guy
这里有一篇文章也解释了这一点。
很抱歉,忘记附加链接。这是我的注释说明
// 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.
这是我的注释说明
// 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.
我本以为我必须调用force才能调用lazy函数?您正在请求值,以便触发变量中的lazy函数运行并分配结果。因为第一个赋值是用lazy包装的,所以它基本上被忽略而没有赋值。但是第二个调用print,然后进行赋值。当你第一次请求它时,它就被分配了。好的。。这是我的一个美好时刻——出于某种原因,我认为这些函数只会在需要时被调用,而且会产生副作用。现在我看了一下,我用C#术语来描述“函数”在默认情况下是按声明顺序创建的,但是惰性函数只有在第一次使用时才被创建,而不管它声明的顺序如何。是的,我知道这些是怎么回事。今天早些时候我有一个,让我疯狂了两天。很高兴提供帮助。我原以为我必须调用force才能调用lazy函数?您正在请求值,以便触发变量中的lazy函数运行并分配结果。因为第一个赋值是用lazy包装的,所以它基本上被忽略而没有赋值。但是第二个调用print,然后进行赋值。当你第一次请求它时,它就被分配了。好的。。这是我的一个美好时刻——出于某种原因,我认为这些函数只会在需要时被调用,而且会产生副作用。现在我看了一下,我用C#术语来描述“函数”在默认情况下是按声明顺序创建的,但是惰性函数只有在第一次使用时才被创建,而不管它声明的顺序如何。是的,我知道这些是怎么回事。今天早些时候我有一个,让我疯狂了两天。很乐意帮忙。