F#:错误FS0030:值限制

MY_*_*Y_G 1 f#

我是编程新手,F#是我的第一语言.

以下是我的代码的相关部分:

let rec splitArrayIntoGroups (inputArray: string[]) (groupSize: int) (hashSetOfGroups: HashSet<string[]>)=
    let startIndex = 0
    let endIndex = groupSize - 1

    let group = inputArray.[startIndex .. endIndex]
    let nextInputArray = inputArray.[groupSize .. inputArray.Length - 1]

    hashSetOfGroups.Add(group) |> ignore
    splitArrayIntoGroups nextInputArray groupSize hashSetOfGroups

let hashSetOfGroups = new HashSet<string[]>()

splitArrayIntoGroups urlArray 10 hashSetOfGroups
Run Code Online (Sandbox Code Playgroud)

urlArray是一个包含近3200个URL的数组.

当我尝试在F#interactive中运行代码时,收到以下错误消息:

Program.fs(119,1):错误FS0030:值限制.值'it'被推断为具有泛型类型val:'_ a将'it'定义为一个简单的数据项,使其成为具有显式参数的函数,或者如果您不打算将其作为通用类型,则添加一个类型注释.

出了什么问题,我应该做出哪些改变?

Gru*_*oon 5

就目前而言,代码将无限循环.什么是退出条件?正如@Petr指出的那样,函数返回了什么?

以下是inputArray为空时退出并返回单位的版本:

let rec splitArrayIntoGroups (inputArray: string[]) (groupSize: int) (hashSetOfGroups: HashSet<string[]>)=

    match inputArray with
    | [||] -> ()
    | _ ->
        let startIndex = 0
        let endIndex = groupSize - 1
        let group = inputArray.[startIndex .. endIndex]
        let nextInputArray = inputArray.[groupSize .. inputArray.Length - 1]

        hashSetOfGroups.Add(group) |> ignore
        splitArrayIntoGroups nextInputArray groupSize hashSetOfGroups
Run Code Online (Sandbox Code Playgroud)

而不是使用可变集合,更惯用的方法是使用F#Set类型然后将新版本传递给每个递归,如下所示:

let rec splitArrayIntoGroups2 inputArray groupSize hashSetOfGroups =

    match inputArray with
    | [||] -> hashSetOfGroups 
    | _ ->
        let startIndex = 0
        let endIndex = groupSize - 1
        let group = inputArray.[startIndex .. endIndex]
        let nextInputArray = inputArray.[groupSize .. inputArray.Length - 1]

        let newSet = Set.add group hashSetOfGroups
        splitArrayIntoGroups2 nextInputArray groupSize newSet 
Run Code Online (Sandbox Code Playgroud)

顺便说一句,它的逻辑似乎是索引逻辑的错误.如果我尝试以下方法:

let urlArray = [| "a"; "b"; "c"; "d" |]
let result = splitArrayIntoGroups2 urlArray 10 Set.empty
Run Code Online (Sandbox Code Playgroud)

然后我得到了IndexOutOfRangeException.

你的意思是这样吗?

let rec splitArrayIntoGroups3 inputArray startIndex groupSize hashSetOfGroups =

    let maxIndex = Array.length inputArray - 1
    if startIndex > maxIndex  then
        hashSetOfGroups 
    else
        let endIndex = min (startIndex + groupSize - 1) maxIndex 
        let group = inputArray.[startIndex .. endIndex]
        let newSet = Set.add group hashSetOfGroups

        let nextStartIndex = endIndex + 1
        splitArrayIntoGroups3 inputArray nextStartIndex groupSize newSet 

let urlArray = [| "a"; "b"; "c"; "d"; "e"  |]
let result = splitArrayIntoGroups3 urlArray 0 2 Set.empty
Run Code Online (Sandbox Code Playgroud)

请注意,此最终版本适用于任何类型的数组,而不仅仅是字符串数组.