Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 在f模式中使用绑定到标识符的值?_F# - Fatal编程技术网

F# 在f模式中使用绑定到标识符的值?

F# 在f模式中使用绑定到标识符的值?,f#,F#,我想要一个F中的递归函数,签名为:string*int*char->int, 一种终止模式是空字符串|,i,c->0, 当s是三元组中的第一项时,第二个应为i=String.length s{ let rec myf (s, i, c) = let lens = String.length s in match s, i, c with | "", i, c -> 0 | s, lens, c -> 0

我想要一个F中的递归函数,签名为:string*int*char->int, 一种终止模式是空字符串|,i,c->0, 当s是三元组中的第一项时,第二个应为i=String.length s{

    let rec myf (s, i, c) =
      let lens = String.length s in
         match s, i, c with
         | "", i,   c -> 0
         | s, lens, c -> 0
         | s, i,    c -> (if s.[i] = c then 1 else 0) + myf(s, i+1, c)
这不起作用,因为第三个图案从不匹配,可能是因为第二个图案中的镜头被视为通配符。是否可以在模式中使用绑定有值的符号,并且模式仅与该值匹配


PS:myf统计索引>=i时c在s中的出现次数

您需要添加保护:

let rec myf (s, i, c) =
    match s, i, c with
    | "", i,   c -> 0
    | s, i, c when s.Length = i -> 0
    | s, i,    c -> (if s.[i] = c then 1 else 0) + myf(s, i+1, c)

您需要添加一个防护:

let rec myf (s, i, c) =
    match s, i, c with
    | "", i,   c -> 0
    | s, i, c when s.Length = i -> 0
    | s, i,    c -> (if s.[i] = c then 1 else 0) + myf(s, i+1, c)