3
votes

I'm writing a Dominoes game for class and can't wrap my head around custom types. I have:

type DomsPlayer = Hand -> Board -> (Domino,End)
...
playTurn :: DomsPlayer -> (Int, Hand, Board)
playTurn hand1 board1 = (score, hand2, board2)
  where (dom, end) = simplePlayer hand1 board1
                     board2 = resMaybe (playDom dom board1 end)
                     hand2 = remove dom hand1
                     score = scoreBoard board2

Trying to load this gives me the errors:

Dominoes.hs:43:3: error:

• Couldn't match expected type ‘(Int, Hand, Board)’ with actual type ‘Board -> (Int, b0, Board)’

• The equation(s) for ‘playTurn’ have two arguments, but its type ‘DomsPlayer -> (Int, Hand, Board)’ has only one

| 43 | playTurn hand1 board1 = (score, hand2, board2) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

Dominoes.hs:44:37: error:

• Couldn't match type ‘Hand -> Board -> (Domino, End)’ with ‘[Domino]’

Expected type: Hand Actual type: DomsPlayer

• Probable cause: ‘hand1’ is applied to too few arguments

In the first argument of ‘simplePlayer’, namely ‘hand1’

In the expression: simplePlayer hand1 board1

In a pattern binding: (dom, end) = simplePlayer hand1 board1

| 44 | where (dom, end) = simplePlayer hand1 board1 |

How do I retrieve the values from DomsPlayer?

1
It looks like the definition of DomsPlayer is a type synonym for a function, not a data type.Zpalmtree
Your type DomsPlayer is a function of two arguments (Hand and Board). Your function playTurn take as argument such a DomsPlayer function and returns a triple. However, your pattern match for playTurn has 2 arguments (hand1 and board1). This does not fit together.mschmidt
I give an answer but in order to help you I want to know what are you trying to do. Do you want to recieve a Hand and a Board? Or do you want a (Domino, End) too? Clearly you are defining a function, because DomsPlayer is a synonym of function.Federico Sawady
I want a (Domino, End) as well.null-skill
it looks a simple type mismatch to me. this should be straightforward confusion. might i kindly ask why people up vote it?Jason Hu

1 Answers

0
votes

If you replace this DomsPlayer with its definition

type DomsPlayer = Hand -> Board -> (Domino,End)

playTurn :: (Hand -> Board -> (Domino,End)) -> (Int, Hand, Board)

you will see that playTurn recieves only one parameter, not two.

I don't know what you are trying to do, but clearly you will need to recieve Hand and Board as separate params:

playTurn :: Hand -> Board -> ... -> (Int, Hand, Board)

And I don't know, maybe also pass a function of type DomsPlayer too, and apply it to those separate arguments.

But that is your error.