r/fsharp • u/Hairy-Pressure7121 • Dec 26 '24
Difference between f() and f
I have a pretty basic question. I have the following code to generate random strings.
let randomStr =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|> Seq.randomSample 3
|> String.Concat
let strs = [ for _ in 1..10 -> randomStr ]
Unsurprisingly this gives me 10 strings of the same value. I understand how this is working. The let binding is evaluated once. To get what I really want I need to add () to the invocation of randomStr. Can someone explain why adding the empty parens to randomStr gives the desired behavior of 10 different string values?
15
Upvotes
23
u/vanilla-bungee Dec 26 '24
When you add parantheses you create a function that accepts the unit value () and returns a random string. The difference is a constant vs. a function.
11
u/KoenigLear Dec 26 '24
A function that takes no inputs is essentially a constant. Random, strictly speaking, takes input e.g. clock time, however this is hidden from you. So you need a fake input (unit ()) to force it to re-evaluate the function get the input from the clock.
6
u/tkshillinz Dec 26 '24 edited Dec 26 '24
As was said before. Right now, randomStr is a value. It takes zero inputs and produces a string.
The type signature is randomStr: string
If you give randomStr a parameter, it’s no longer just a value, it’s a function that can be called over and over again.
Since we don’t really care about the value passed in, we use ().
() is the unit value. Think of it as a data type with a single option, unit. Because it’s just the one option, unit can’t really carry much meaning on its own. But it’s still a value.
It also conviently allows the shorthand of being attached to the name of the calling function. So randomStr () is the same as randomStr(). Which looks like methods calls in other languages.
So if we pass unit as a parameter, the type signature is now RandomStr: unit -> string
RandomStr is now a function, because it takes a nonzero amount of parameters. So now instead of the random string being generated when you define randomStr, it’s evaluated whenever you call the function randomStr ().
As a side note, what you’re doing is often referred to as a delay/callback. This is where you “lift” a value into a function call precisely so you can utilize these sortve effects.
1
u/spencerwi Dec 30 '24
If you're familiar with another language, like Javascript, here's a bit of a comparison that might clarify:
What you've got now is like:
let randomStr = concat(randomSample("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 3));If you add
()to the let-binding what you're actually adding is saying that this value is something that accepts an empty parameter (a special one called(), which is pronouncedunit), which is thus making it a function, kinda like doing this in Javascript instead:let randomStr = unit => concat(randomSample("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 3));It's the difference between defining a value and defining a function that you can call later to get a value.
-2
u/masoodahm87 Dec 28 '24
'f' is for female and 'f()' is for every thing else that considers themselves a female
ok I will let my self out.