I recently ran into an interesting but annoying F Sharp behavior. According to [1], “F# automatically caches the value of any function which takes no parameters.” This seems like a good idea, but it is causing problems for me as I try to come up with a wrapper function to generate random numbers.
As an example, I have two different functions in the code at the end of this question. The first function “getRand” takes no parameters, but unfortunately it always returns the same number. The second function “getRand2” works as I would expect generating a new random number each time it is called, but it annoyingly takes a useless and ignored extra parameter.
If possible, I would like to have the functionality of getRand2 but the convenience of getRand. Is there a compiler directive or special keyword I can apply to getRand that will turn off its function caching capabilities and thereby cause it to behave like getRand2?
With Thanks,
Shawn
Note: Forgive me if the answer already appears in [1], I’m just not seeing it right now.
[1] – http://en.wikibooks.org/wiki/F_Sharp_Programming/Caching
(* Always returns the same number *)
let getRand =
let seed = int32(System.DateTime.Now.Ticks)
let randGen = new System.Random(seed)
randGen.Next()
(* Works as expected except I need an annoying extra parameter *)
let getRand2 dummyParam =
let seed = int32(System.DateTime.Now.Ticks)
let randGen = new System.Random(seed)
randGen.Next()
(* Outputs three "identical" numbers to console *)
System.Console.WriteLine(
"Parameterless getRand always outputs same number.")
System.Console.WriteLine(getRand)
System.Threading.Thread.Sleep(100)
System.Console.WriteLine(getRand)
System.Threading.Thread.Sleep(100)
System.Console.WriteLine(getRand)
System.Console.WriteLine()
(* Outputs three "different" numbers to console *)
System.Console.WriteLine(
"GetRand2 works as expected even though second dummy param is always the same.")
System.Console.WriteLine(getRand2 0)
System.Threading.Thread.Sleep(100)
System.Console.WriteLine(getRand2 0)
System.Threading.Thread.Sleep(100)
System.Console.WriteLine(getRand2 0)
System.Console.WriteLine()
Just to clarify a bit, I think that the phrase “function which takes no parameters” is misleading. By definition, a function maps a value from the function’s domain to a value in the function’s range, so all functions take parameters. In your case,
getRandis not bound to a function, it’s just a value of typeint.If I understand your question correctly, I think you want to do
You still need to call
getRandas a function (getRand(), not justgetRand), but there’s no way to work around this – the fact that anintvalue always remains the same is a critical feature for reasoning about a program.You could use your
getRand2function in much the same way as my version ofgetRand: since you don’t usedummyParamwithin the body, F# makes the function generic, which means that you can pass the unit value()as the argument if you want. However, yourgetRand2function is broken, in that it creates a new random number generator each time it is called. This means that if you call it twice within one tick, you will get the same answer:That’s why it’s important to define
seedandrandGenoutside of the scope of the anonymous function.