gpt4 book ai didi

f# - 如何创建一个函数来检查被区分联合的标签是否匹配?

转载 作者:行者123 更新时间:2023-12-04 14:42:02 24 4
gpt4 key购买 nike

假设我们在 F# 中有一个 uniontype:

type Example =
|FirstLabel of int
|SecondLabel of int
|ThirdLabel of int

你怎么能创建一个函数,它接受 2 个“示例”类型的参数并返回 true,如果这两个参数共享相同的标签,否则返回 false?无论整数的值如何,我都希望此函数返回这些结果。

所以如果我们有参数1和参数2
val parameter1 : Example = SecondLabel 2


val parameter2 : Example = Secondlabel 5

该函数将返回 true
即使通过彻底搜索,我也无法找到这个问题的答案。也许我搜索错了。那么你能不能给我一个解决这些问题的来源?

最佳答案

let sameLabels x y = 
match x, y with
| FirstLabel _ , FirstLabel _
| SecondLabel _, SecondLabel _
| ThirdLabel _ , ThirdLabel _ -> true
| _ -> false

关于f# - 如何创建一个函数来检查被区分联合的标签是否匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35687142/

24 4 0