函数中的初学者类型错误

时间:2013-01-14 15:40:46

标签: haskell types

在此功能中:

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

其中一个代理人可能是:

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

我收到错误:

    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)'

在我看来,agent1的返回类型应该是布尔列表,但似乎有错误。如果这是一个非常初学的问题,我很抱歉。谢谢

1 个答案:

答案 0 :(得分:4)

(++)需要两个列表,但是你的agent函数只返回一个bool。尝试

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

如果您在历史记录中存储项目的顺序无关紧要,使用(:)会更有效,即

play history agent1 agent2  = agent1 history : agent2 history : history
相关问题