gpt4 book ai didi

parsing - F# 中的事件模式的递归如何工作?

转载 作者:行者123 更新时间:2023-12-02 14:08:09 26 4
gpt4 key购买 nike

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

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)

*error FS0001: This expression was expected to have type char list * 'a but here has type char list

最佳答案

let rec (|Digits|) = function
| head::(Digits (a, b)) when System.Char.IsDigit(head) -> (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)

关于parsing - F# 中的事件模式的递归如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25588208/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com