F# 使用F中的选项类型实现tryFindIndex#

F# 使用F中的选项类型实现tryFindIndex#,f#,F#,我不熟悉函数式语言编程。我正在尝试实现F#tryfindex函数 let rec tryFindIndex func list = match list with | [] -> None | hd::tl -> if func hd then Some(0) else (tryFindIndex func tl) + 1 问题在于最后一行,因为添加1会导致返回类型为“int”而不是“int option”。我需要递归跟踪索引

我不熟悉函数式语言编程。我正在尝试实现F#tryfindex函数

let rec tryFindIndex func list =
    match list with
    | [] -> None
    | hd::tl -> if func hd then Some(0)
                else (tryFindIndex func tl) + 1

问题在于最后一行,因为添加1会导致返回类型为“int”而不是“int option”。我需要递归跟踪索引。

将索引作为附加参数传递。如果不这样做,那么函数也不是尾部递归的。还将递归实现为一个单独的循环,以隐藏索引参数

let tryFindIndex func list =
    let rec loop func list index =
        match list with
        | [] -> None
        | hd::tl -> if func hd then Some(index)
                    else loop func tl (index+1)
    loop func list 0
正如John在评论中所指出的,它的核心库实现如下所示:

    let tryFindIndex f list = 
        let rec loop n = function[] -> None | h::t -> if f h then Some n else loop (n+1) t
        loop 0 list

将索引作为附加参数传递。如果不这样做,那么函数也不是尾部递归的。还将递归实现为一个单独的循环,以隐藏索引参数

let tryFindIndex func list =
    let rec loop func list index =
        match list with
        | [] -> None
        | hd::tl -> if func hd then Some(index)
                    else loop func tl (index+1)
    loop func list 0
正如John在评论中所指出的,它的核心库实现如下所示:

    let tryFindIndex f list = 
        let rec loop n = function[] -> None | h::t -> if f h then Some n else loop (n+1) t
        loop 0 list

@对不起。在你的编辑生效之前,我一直在编辑它!完美的我认识到需要一个额外的参数,但没有意识到嵌套let语句可以解决这个问题。谢谢@airietis嵌套let允许您定义具有本地作用域的函数,然后在该作用域内调用它。很乐意帮忙。还修复了您的
部分
中的索引。再次感谢。此外,循环函数需要是递归的(添加'rec')。Oops。忘记将其移动到内部循环。@snf抱歉。在你的编辑生效之前,我一直在编辑它!完美的我认识到需要一个额外的参数,但没有意识到嵌套let语句可以解决这个问题。谢谢@airietis嵌套let允许您定义具有本地作用域的函数,然后在该作用域内调用它。很乐意帮忙。还修复了您的
部分
中的索引。再次感谢。此外,循环函数需要是递归的(添加'rec')。Oops。忘了把它移到内部循环。实际上,您可以查看F#源代码,找到它是如何为编译器实现的。TryFindIndex在这里@John Palmer很酷,谢谢!实际上,您可以查看F#源代码,了解它是如何为编译器实现的。TryFindIndex在这里@John Palmer很酷,谢谢!