递归如何适用于F#中的活动模式?

yxr*_*rkt 4 f# parsing metaprogramming

我有以下函数来解析整数:

let rec digits = function
    | head::tail when System.Char.IsDigit(head) ->
        let result = digits tail
        (head::(fst result), snd result)
    | rest -> ([], rest)
Run Code Online (Sandbox Code Playgroud)

如果我将此函数更改为活动识别器,则不再编译.

let rec (|Digits|) = function
    | head::tail when System.Char.IsDigit(head) ->
        let result = Digits tail
        (head::(fst result), snd result)
        //          ^^^^^^       ^^^^^^ see error*
    | rest -> ([], rest)
Run Code Online (Sandbox Code Playgroud)

*错误FS0001:此表达式应该具有char list*'a类型,但这里有char列表类型

des*_*sco 7

let rec (|Digits|) = function
    | head::(Digits (a, b)) when System.Char.IsDigit(head) -> (head::a, b)
    | rest -> ([], rest)
Run Code Online (Sandbox Code Playgroud)

注意:如果您想将活动模式用作函数,您仍然可以执行此操作:

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