我正在嘗試學習haskell,並且存在一個我無法弄清楚的特定錯誤。有人可以解釋這個錯誤?無法匹配'Integer'類型
robot (_name,_attack,_hp) = \ cmd -> cmd (_name,_attack,_hp)
hp (_,_,h) = h
getHp aRobot = aRobot hp
setHp aRobot newHp = aRobot (\ (n,a,_) -> robot (n,a,newHp))
damage aRobot amount = let actualHp = getHp aRobot
in
setHp aRobot (actualHp - amount)
makeKiller = robot ("Killer",10,200)
makeBetty = robot ("Betty",5,300)
----- Example of computation in ghci
b = makeBetty
b1 = damage b 34
<interactive>:52:14: error:
* Couldn't match type `Integer'
with `(([Char], Integer, t1) -> t0) -> t0'
Expected type: (([Char], Integer,
(([Char], Integer, t1) -> t0) -> t0)
-> (([Char], Integer, t1) -> t0) -> t0)
-> t1
Actual type: (([Char], Integer, Integer) -> t1) -> t1
* In the first argument of damage, namely `b'
In the expression: damage b 34
In an equation for `b1': b1 = damage b 34
* Relevant bindings include b1 :: t1 (bound at <interactive>:52:1)
有什麼損壞功能是錯誤的,任何人都可以向我解釋錯誤嗎?
預先感謝您!
添加類型簽名。令人驚訝的是,只需在'hp'中添加一個類型簽名,並且您會注意到'aRobot'在某些情況下具有的類型。 – Zeta