F # is for .. inequivalent to a C # loop foreach, not C # for. And since C # 5, the loop foreachdoes not update the iterator; instead, it creates a new variable for each iteration through the loop (this has important consequences for closure, see Foreach now captures variables! (Access to modified closure) for more information. It also does F #: if you write
for txt in ["abc"; "def"; "ghi"] do
printfn "%s" txt
then you actually create three new string variables when you run this loop, and not just one.
To make sure F # creates a new variable every time, try the following not-so-functional code:
let actions = new System.Collections.Generic.List<System.Action<unit>>()
for txt in ["abc"; "def"; "ghi"] do
actions.Add(fun () -> printf "%s " txt)
for action in actions do
action.Invoke()
txt for .. in, ghi ghi ghi, # 4 , , ghi. , , abc def ghi, # 5 .
, , F # for .. in , .