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)