gpt4 book ai didi

f# - 在 F# 中比较带有 < 和 > 的可区分联合案例

转载 作者:行者123 更新时间:2023-12-05 01:25:47 26 4
gpt4 key购买 nike

我正在学习 F#,并且正在构建一组快速函数,用于比较两手牌并确定获胜者。

我创建了这个有区别的联合来代表扑克手的类别:

type Category =
| HighCard
| OnePair
| TwoPair
| ThreeOfAKind
| Straight
| Flush
| FullHouse
| FourOfAKind
| StraightFlush

我使用此代码比较类别以确定一只手是否优于另一只手:

if playerCategory > houseCategory then Win
elif playerCategory < houseCategory then Loss
// ... More code to handle cases within the same category

因此,例如,表达式:

let playerCategory = FullHouse
let houseCategory = HighCard
if playerCategory > houseCategory then Win
elif playerCategory < houseCategory then Loss
// ... Other code

会有值 Win

但是,我不明白 < 和 > 运算符如何在这里工作。 (最初我有一个函数将每个案例映射到一个数值,但我意识到这没有必要。)如果我重新排列案例的顺序,那么逻辑就会中断,所以我假设每个案例都被分配了一些默认值对应于它在类型内的顺序?

但我肯定会感激更多的见解......

最佳答案

这在规范中有描述:

by default, record, union, and struct type definitions called structural types implicitly include compiler-generated declarations for structural equality, hashing, and comparison. These implicit declarations consist of the following for structural equality and hashing

8.15.4 生成的 CompareTo 实现的行为

If T is a union type, invoke Microsoft.FSharp.Core.Operators.compare first on the index of the union cases for the two values, and then on each corresponding field pair of x and y for the data carried by the union case. Return the first non-zero result.

关于f# - 在 F# 中比较带有 < 和 > 的可区分联合案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25317142/

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