在for循环中增加1

Jac*_*cco 0 f#

我有一个像这样简单的for循环

let mutable index = 0
let mutable indx = 0
for c in list do 
    //some code
    index <- indx
    indx <- indx + 1
Run Code Online (Sandbox Code Playgroud)

基本上我想要做的是迭代对象列表并在列表中搜索特定对象,然后将索引变量设置为我正在寻找的对象的索引.

我假设它与最后一行有关,我认为我将indx递增1,但它似乎不起作用.

pli*_*nth 5

为什么不进行没有突变的功能方法?

let my_predicate item = // some condition on item

let index = list |> Seq.findIndex my_predicate

// index will be bound to the first item in list for which my_predicate returns true
Run Code Online (Sandbox Code Playgroud)