gpt4 book ai didi

f# - 元组列表上的模式匹配

转载 作者:行者123 更新时间:2023-12-05 00:17:31 24 4
gpt4 key购买 nike

如果我想添加元组列表的所有元素,我会收到以下错误

let rec addTupLst (xs: 'a * 'a list) =
match xs with
| (a, b) :: rst -> a + b + (addTupLst rst)
| _ -> 0

addTupLst [(1, 2)]

我收到警告

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



是否不可能以这种方式在元组列表上进行模式匹配,还是存在另一个错误?

最佳答案

你只是忘记了一对括号

let rec addTupLst (xs: ('a * 'a) list) =
match xs with
| (a, b) :: rst -> a + b + (addTupLst rst)
| _ -> 0

addTupLst [(1, 2)]

关于f# - 元组列表上的模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40177735/

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