F# F:错误FS0030:值限制

F# F:错误FS0030:值限制,f#,F#,我是编程新手,F是我的第一语言 以下是我的代码的相关部分: let rec splitArrayIntoGroups (inputArray: string[]) (groupSize: int) (hashSetOfGroups: HashSet<string[]>)= let startIndex = 0 let endIndex = groupSize - 1 let group = inputArray.[startIndex .. endIndex

我是编程新手,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
urlArray是一个包含近3200个URL的数组

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

Program.fs119,1:错误FS0030:值限制。“it”的价值 已推断为具有泛型类型 val it:“\u将“it”定义为一个简单的数据项,使其成为一个具有显式参数的函数,或者,如果您不希望它 如果是泛型,请添加类型批注


出了什么问题,我应该做什么更改?

目前,代码将无限循环。退出条件是什么?正如@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
顺便说一句,目前的逻辑似乎与索引逻辑有缺陷。如果我尝试以下方法:

let urlArray = [| "a"; "b"; "c"; "d" |]
let result = splitArrayIntoGroups2 urlArray 10 Set.empty
然后我得到一个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

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

目前,代码将无限循环。退出条件是什么?正如@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
顺便说一句,目前的逻辑似乎与索引逻辑有缺陷。如果我尝试以下方法:

let urlArray = [| "a"; "b"; "c"; "d" |]
let result = splitArrayIntoGroups2 urlArray 10 Set.empty
然后我得到一个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

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

splitArrayIntoGroups函数应该返回什么值?如果它只是产生副作用,它应该返回单位,但在这种情况下,我想它不会正常终止?若它只是产生副作用,它应该返回单位,但在这种情况下,我想它不会正常终止。