简体   繁体   English

了解Haskell中的类型

[英]Trouble understanding types in Haskell

I'm given the following where: 我得到以下内容:

data Card = Card Suit Rank
  deriving (Eq, Ord, Show)

type BidFunc
  = Card    -- ^ trump card
  -> [Card] -- ^ list of cards in the player's hand
  -> Int    -- ^ number of players
  -> [Int]  -- ^ bids so far
  -> Int    -- ^ the number of tricks the player intends to win

where I'm required to write a function of 我需要在其中编写函数的地方

makeBid :: BidFunc
makeBid = (write here)

The problem I'm having is that i couldnt understand the syntax of the function type declared which is BidFunc. 我遇到的问题是我无法理解所声明的函数类型BidFunc的语法。 I'm new to Haskell so i would appreciate if someone could give me an explanation clear enough on the function type above. 我是Haskell的新手,所以如果有人可以对上述函数类型给我一个足够清晰的解释,我将不胜感激。

In particularly, why is there a '=' Card, followed by -> [Card] etc? 特别是为什么会有'='卡,然后是-> [Card]等? Am i supposed to pass in arguments to the function type? 我应该将参数传递给函数类型吗?

makeBid :: BidFunc is exactly the same as makeBid :: Car -> [Card] -> Int -> [Int] -> Int , so you would define the function in exactly the same way: makeBid :: BidFunc完全一样的makeBid :: Car -> [Card] -> Int -> [Int] -> Int ,所以你会在完全相同的方式定义函数:

makeBid :: BidFunc
-- makeBid :: Card -> [Card] -> Int -> [Int] -> Int
makeBid c cs n bs = ...

As for the formatting of the type definition, it's just that: formatting. 至于type定义的格式,仅此而已:格式。 IMO, it would be a little clearer written as 海事组织,写成

type BidFunc = Card   -- ...
            -> [Card]  -- ...
            -> Int    -- ...
            -> [Int]  -- ...
            -> Int    -- ...

if you want to comment on each argument and the return value. 如果要对每个参数和返回值进行注释。 Without comments, it can of course be written on one line: 没有注释,它当然可以写在一行上:

type BidFunc = Card -> [Card] -> Int -> [Int] -> Int

In general, type <lhs> = <rhs> just remeans that <lhs> is a name that can refer to whatever type <rhs> specifies. 通常, type <lhs> = <rhs>仅表示<lhs>是可以引用<rhs>指定的任何类型的名称。


As to why one might feel the need to define a type alias for something that isn't going to be reused often, I couldn't say. 至于为什么可能会觉得需要为不经常重复使用的东西定义类型别名的原因,我不能说。 Are they any other functions beside makeBid that would have the same type? 除了makeBid ,它们是否还有其他具有相同类型的函数?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM