I have a question on why I am getting certain results in F#. I have the following code…
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
My output is as follows…
Active Hello World
Lazy Hello World
lazy value is 60
lazy value is 60
active value is 60
active value is 60
What I can’t understand is this… Why is active hello world’s printfn being shown before lazy hello word? I would have expected “Lazy Hello World” to have been shown before “Active Hello World”?
If anyone can help explain this it would be much appreciated.
Well I am not a big F# person but with lazy patterns it is set so they don’t do anything until they are request. So when you wrap that in lazy it won’t run until you actually use it. But since you didn’t use that for the active one it performed the function as soon as it was assigned.
This is just a guess though as I am not a F# guy.
Here is an article that explains it as well.
http://weblogs.asp.net/podwysocki/archive/2008/03/21/adventures-in-f-f-101-part-6-lazy-evaluation.aspx
Sorry forgot to attach the link.