Maxxx
Maxxx

Reputation: 3768

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. I'm new to Haskell so i would appreciate if someone could give me an explanation clear enough on the function type above.

In particularly, why is there a '=' Card, followed by -> [Card] etc? Am i supposed to pass in arguments to the function type?

Upvotes: 0

Views: 130

Answers (1)

chepner
chepner

Reputation: 532418

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 :: Card -> [Card] -> Int -> [Int] -> Int
makeBid c cs n bs = ...

As for the formatting of the type definition, it's just that: formatting. 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.


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?

Upvotes: 7

Related Questions