0

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?

1 Answer 1

7

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?

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for the thorough walkthrough. One question, do i need to define the return value as well which is the final -> Int in the function type in makeBid c cs n bs or is it just the first four arguments?
The return value is just whatever the expression the right side evaluates two. For example, a valid (though probably useless) definition might be makeBid c cs n bs = 3, since 3 is a valid Int value.
thanks but I'm still terribly confused on how the BidFunc returns an Int from the 4 set of arguments that is supplied. The game is based off "Oh Hell" card game where the player makes a bid at start. So in a relatively simple example, say i wrote something like "makeBid c cs n bs = (for card in cs, if card == c) then doSomething...". I understand if it's "add x y = x + y" which would give the summation of x and y but for a card game, it's just not as clear as i expected.
This is getting pretty far off-topic, as you are now asking how one would actually implement makeBid, rather than asking about the syntax. Consider a very simple definition, though, that simply outbids the last player as long as it's theoretically possible to take that many tricks. You might write makeBid _ hand _ (lastbid:_) = if lastbid == length hand then 0 else lastbid + 1. This ignores the proposed trump suit and the number of players, and it assumes that at least one bid has been made (and that the most recent bid is at the front of the list).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.