繁体   English   中英

了解Haskell中的类型

[英]Trouble understanding types in Haskell

我得到以下内容:

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

我需要在其中编写函数的地方

makeBid :: BidFunc
makeBid = (write here)

我遇到的问题是我无法理解所声明的函数类型BidFunc的语法。 我是Haskell的新手,所以如果有人可以对上述函数类型给我一个足够清晰的解释,我将不胜感激。

特别是为什么会有'='卡,然后是-> [Card]等? 我应该将参数传递给函数类型吗?

makeBid :: BidFunc完全一样的makeBid :: Car -> [Card] -> Int -> [Int] -> Int ,所以你会在完全相同的方式定义函数:

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

至于type定义的格式,仅此而已:格式。 海事组织,写成

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

如果要对每个参数和返回值进行注释。 没有注释,它当然可以写在一行上:

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

通常, type <lhs> = <rhs>仅表示<lhs>是可以引用<rhs>指定的任何类型的名称。


至于为什么可能会觉得需要为不经常重复使用的东西定义类型别名的原因,我不能说。 除了makeBid ,它们是否还有其他具有相同类型的函数?

暂无
暂无

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

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