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
Parsing 递归是如何在F#中为活动模式工作的?_Parsing_F#_Metaprogramming - Fatal编程技术网

Parsing 递归是如何在F#中为活动模式工作的?

Parsing 递归是如何在F#中为活动模式工作的?,parsing,f#,metaprogramming,Parsing,F#,Metaprogramming,我有以下用于解析整数的函数: let rec digits = function | head::tail when System.Char.IsDigit(head) -> let result = digits tail (head::(fst result), snd result) | rest -> ([], rest) 如果我将此函数更改为活动识别器,它将不再编译 let rec (|Digits|) = function

我有以下用于解析整数的函数:

let rec digits = function
    | head::tail when System.Char.IsDigit(head) ->
        let result = digits tail
        (head::(fst result), snd result)
    | rest -> ([], rest)
如果我将此函数更改为活动识别器,它将不再编译

let rec (|Digits|) = function
    | head::tail when System.Char.IsDigit(head) ->
        let result = Digits tail
        (head::(fst result), snd result)
        //          ^^^^^^       ^^^^^^ see error*
    | rest -> ([], rest)
*错误FS0001:此表达式应具有类型char list*'a,但此处具有类型 字符表

注: 如果您想使用活动模式作为功能,您仍然可以这样做:

let rec (|Digits|) = function
    | head::tail when System.Char.IsDigit(head) ->
        let a, b = (|Digits|) tail
        (head::a, b)
    | rest -> ([], rest)

啊,我明白了。我只是在看。这是有道理的,因为返回类型也是规则的一部分(如果我的术语没有错的话)。非常感谢!
let rec (|Digits|) = function
    | head::tail when System.Char.IsDigit(head) ->
        let a, b = (|Digits|) tail
        (head::a, b)
    | rest -> ([], rest)