fsharp 中受歧视工会的列表

发布于 2025-01-03 16:19:32 字数 558 浏览 1 评论 0原文

有人能解释为什么下面的 2 个 let 语句不起作用吗?

 type Rank =
 | Two
 | Three
 | Four
 | Five
 | Six
 | Seven
 | Eight
 | Nine
 | Ten

 type Face =
 | Jack
 | Queen
 | King
 | Ace

 type Suit =
 | Diamonds
 | Clubs
 | Hearts
 | Spades

 type Card =
 | RankCard of Rank * Suit
 | FaceCard of Face * Suit


 let deck : Card = [ (Two, Diamonds); (Jack, Hearts) ]

该表达式应具有 Card 类型,但此处具有 'a list 类型

,此 let 给出的

 let deck : Card list = [ (Two, Diamonds); (Jack, Hearts) ]

表达式应具有 Card 类型,但此处具有 'a * 'b 类型

Can anybody explain why following 2 let statements don't work?

 type Rank =
 | Two
 | Three
 | Four
 | Five
 | Six
 | Seven
 | Eight
 | Nine
 | Ten

 type Face =
 | Jack
 | Queen
 | King
 | Ace

 type Suit =
 | Diamonds
 | Clubs
 | Hearts
 | Spades

 type Card =
 | RankCard of Rank * Suit
 | FaceCard of Face * Suit


 let deck : Card = [ (Two, Diamonds); (Jack, Hearts) ]

This expression was expected to have type Card but here has type 'a list

and this let gives

 let deck : Card list = [ (Two, Diamonds); (Jack, Hearts) ]

expression was expected to have type Card but here has type 'a * 'b

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼睛会笑 2025-01-10 16:19:32

F# 是一种类型安全语言。因此第一个表达式是错误的,因为 Card'a list 不兼容。第二个表达式也不正确,因为您的注释需要 Card 类型的列表元素,但您提供了元组。

此外,(Two, Diamonds)(Jack, Hearts) 在同一列表中使用甚至不合法。前者是Rank * Suit的元组,后者是Face * Suit的元组。

您的意图是创建两个 Card 类型的值;您必须根据 Card 的不同联合情况提供适当的构造函数:

let c1 = RankCard (Two, Diamonds)  // c1: Card
let c2 = FaceCard (Jack, Hearts) // c2: Card

现在您可以在同一列表 deck 中使用 c1c2,并且 F# 类型检查器将自动推断 deck 具有 Card list 类型:

let deck = [c1; c2] // deck: Card list

或者,您有一个列表,如下所示:

let deck = [RankCard (Two, Diamonds); FaceCard (Jack, Hearts)]

F# is a type-safe language. So the first expression is wrong since Card and 'a list are incompatible. The second expression is also incorrect because your annotation requires list elements in Card type but you provided tuples instead.

Moreover, (Two, Diamonds) and (Jack, Hearts) are not even legal to use in the same list. The former is a tuple of Rank * Suit and the latter is a tuple of Face * Suit.

Your intention is creating two values of type Card; you have to provide appropriate constructors based on different union cases of Card:

let c1 = RankCard (Two, Diamonds)  // c1: Card
let c2 = FaceCard (Jack, Hearts) // c2: Card

Now you can use c1 and c2 in the same list deck, and F# type checker will automatically infer deck to have the type of Card list:

let deck = [c1; c2] // deck: Card list

Alternatively, you have a list as follows:

let deck = [RankCard (Two, Diamonds); FaceCard (Jack, Hearts)]
蓝戈者 2025-01-10 16:19:32

您需要使用 RankCardFaceCard 构造函数 - 否则 F# 认为您刚刚给了它一个正常的元组列表。

或者,为什么不让 F# 自行推断类型呢?

You need to use the RankCard or FaceCard constructor -- otherwise F# thinks you've just given it a normal list of tuples.

Alternatively, why not let F# infer the types itself?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文