another noob question regarding F#.
If I have the following code…
let ExeC =
printfn "c"
3
let ExeB b =
printfn "b"
2
let ExeA =
printfn "a"
1
printfn "Example %d " ExeA
printfn "Example %d " (ExeB 1)
printfn "Example %d " ExeC
The output is as follows…
c
a
Example 1
b
Example 2
Example 3
What seems unusual here is the order that the code is executing in. In a previous question Brian mentioned something about expressions, I was hoping someone could explain this a bit more. It almost seems like the compiler is intelligently pre-executing things to calculate values… but I don’t know?
ExeAandExeCaren’t functions, but single values. The compiler ensures that values initialise in the order in which they’re declared in the source file, so what’s happening here is:ExeCinitialisesExeAinitialisesExample 1is printed, usingExeA‘s initialised valueExeBfunction is called as normalExample 3is printed, usingExeC‘s initialised valueIf you want
ExeAandExeCto be truly lazy — that is, to control when their side effects run — you could turn them into functions that acceptunit: