open System
let repeat action count =
for i in 1..count do
action
let print =
printfn "Test"
repeat print 10
Results in "Test" being printed only once. How should it be written to print 10 times? If the 10 is currently unused why are there no warnings?
CodePudding user response:
The problem is that you're not defining print
as a function but a constant value.
Functions should take at least one unit
parameter:
open System
let repeat action count =
for i in 1..count do
action ()
let print () =
printfn "Test"
repeat print 10
In your original code, the side-effect of printing happens only once, at the definition of print
.
And there are no warnings as print
it is used, but its value is ()
, which is the value returned by the printfn "test"
function call.
CodePudding user response:
let print =
printfn "Test"
This is a binding. It means that variable print is assigned to result of execution of printfn "Test"
. Result of execution of printfnt "Test"
is a unit ()
, which leads to following situation: print
equals unit
In your loop you just return unit with each iteration, without actually calling anything.
To get desired results you need to change binding into a function:
open System
let repeat action count =
for i in 1..count do
action()
let print() =
printfn "Test"
repeat print 10