I test some simple F# code for “if” expression, but the result is unexpected for me:
> let test c a b = if c then a else b;;
val test : bool -> 'a -> 'a -> 'a
However
> test true (printfn "a") (printfn "b");;
a
b
val it : unit = ()
I’d expect only “a” is printed out but here I got both “a” and “b”. I wonder why it comes out this way? Thanks!
Possibly because both printfn function calls are evaluated before the test call ever occurs? If you want both the function calls to be delayed until they’re actually used, you might want lazy computation or macros (which F# doesn’t have).