pro*_*Fun 6 f# discriminated-union
假设我们在F#中有一个uniontype:
type Example =
|FirstLabel of int
|SecondLabel of int
|ThirdLabel of int
Run Code Online (Sandbox Code Playgroud)
你怎么能创建一个函数,它接受"Example"类型的2个参数并返回true,如果这两个参数共享相同的标签,否则返回false?我希望此函数返回这些结果,而不管整数的值.
所以如果我们有参数1和参数2
val parameter1 : Example = SecondLabel 2
Run Code Online (Sandbox Code Playgroud)
和
val parameter2 : Example = Secondlabel 5
Run Code Online (Sandbox Code Playgroud)
该函数将返回 true
即使彻底搜索,我也找不到这个问题的答案.也许我错了.那么你能给我一个解决这些问题的来源吗?
let sameLabels x y =
match x, y with
| FirstLabel _ , FirstLabel _
| SecondLabel _, SecondLabel _
| ThirdLabel _ , ThirdLabel _ -> true
| _ -> false
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46 次 |
| 最近记录: |