这在ghci中工作正常:
printRobot (fight killerRobot gentleGiant)
但这会引发“没有要显示的实例”错误,而且我似乎看不出原因。
threeRoundFight a b =
(\a b -> printRobot (fight a b))
这是错误:
• No instance for (Show
((((t40, t50, t50) -> t50) -> t60)
-> (((t20, t10, t60) -> ((t20, t10, t60) -> t0) -> t0)
-> (([Char], a10, a0) -> [Char]) -> t30)
-> t30))
arising from a use of ‘print’
(maybe you haven't applied a function to enough arguments?)
• In the first argument of ‘print’, namely ‘it’
In a stmt of an interactive GHCi command: print it
以下是需要考虑的必要功能:
fight aRobot defender = damage defender attack
where attack = if getHP aRobot > 10
then getAttack aRobot
else 0
printRobot aRobot = aRobot(\(n,a,h)->n ++ " attack:" ++ (show a) ++ " hp: " ++ (show h))
robot (name,attack,hp) = \message -> message (name,attack,hp)
这里是机器人(我作为参数输入的内容,即killerRobot和gentleGiant):
killerRobot = robot ("killer",25,200)
gentleGiant = robot ("Mr.Friendly", 10, 300)
您也可以看到,我正在尝试通过嵌套Lambda覆盖机器人,使其成为 3Round 战斗。但是我似乎不明白如果我能从第一场战斗开始的话该如何继续下去。
答案 0 :(得分:7)
行
threeRoundFight a b = (\a b -> printRobot (fight a b))
等同于
threeRoundFight x y = (\a b -> printRobot (fight a b))
-- ^^^ unused variables
(如果启用警告,GHC会帮助您检测到此错误)
这不是您想要的。您可能想要
threeRoundFight = (\a b -> printRobot (fight a b))
-- or
threeRoundFight a b = printRobot (fight a b)