MichaelFine
MichaelFine

Reputation: 121

Beginner Type Error in Function

In this function:

play :: [Bool] -> ([Bool] -> Bool) -> ([Bool] -> Bool) -> [Bool]
play history agent1 agent2  = history ++ (agent1 history) ++ (agent2 history)

Where one of the agents may be:

titForTat :: [Bool] -> Bool
titForTat history
    | last history = True
    | otherwise    = False

I get the error:

    Couldn't match expected type `[Bool]' with actual type `Bool'
    In the return type of a call of `agent1'
    In the first argument of `(++)', namely `(agent1 history)'
    In the second argument of `(++)', namely
      `(agent1 history) ++ (agent2 history)'

It looks to me that the return type of agent1 should be a list of Booleans, but there seems to be an error. I'm sorry if this is a very beginner question. Thank you

Upvotes: 1

Views: 79

Answers (1)

Frerich Raabe
Frerich Raabe

Reputation: 94539

(++) expects two lists, but your agent functions just return a bool. Try

play history agent1 agent2  = history ++ [agent1 history] ++ [agent2 history]

If the order in which you store items in your history doesn't matter, using (:) would be more efficient, i.e.

play history agent1 agent2  = agent1 history : agent2 history : history

Upvotes: 4

Related Questions