Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- robot (name, attack, hp) = \message -> message (name, attack, hp)
- name (n, _, _) = n
- attack (_, a, _) = a
- hp (_, _, hp) = hp
- getName aRobot = aRobot name
- setName aRobot newName = aRobot (\(n, a, h) -> robot (newName, a, h))
- getAttack aRobot = aRobot attack
- setAttack aRobot newAttack = aRobot (\(n, a, h) -> robot (n, newAttack, h))
- getHP aRobot = aRobot hp
- setHP aRobot newHP = aRobot (\(n, a, h) -> robot (n, a, newHP))
- printRobot aRobot = aRobot (\(name, attack, hp) -> name ++ " attack:" ++ (show attack) ++ " hp:" ++ (show hp))
- damage aRobot attackDamage =
- aRobot (\(n, a, h) -> robot (n, a, h - attackDamage))
- fight aRobot defender = damage defender attack
- where attack = if getHP aRobot > 10
- then getAttack aRobot
- else 0
- killerRobot = robot ("Kill3r", 25, 200)
- gentleGiant = robot ("Mr. Friendly", 10, 300)
- gentleGiantRound1 = fight killerRobot gentleGiant
- killerRobotRound1 = fight gentleGiant killerRobot
- gentleGiantRound2 = fight killerRobotRound1 gentleGiantRound1
- killerRobotRound2 = fight gentleGiantRound1 killerRobotRound1
- gentleGiantRound3 = fight killerRobotRound2 gentleGiantRound2
- killerRobotRound3 = fight gentleGiantRound2 killerRobotRound2
- -- doesn't work as expected, cannot print the resulting robot
- getWinner aRobot bRobot = if ahp > bhp then aRobot else bRobot
- where bhp = getHP bRobot
- ahp = getHP aRobot
- -- doesn't work compile at all
- fightOneRound robotA robotB = (robotA', robotB')
- where robotA' = fight robotA robotB
- robotB' = fight robotB robotA
- {-
- robot.hs:48:26: error:
- • Occurs check: cannot construct the infinite type:
- a1 ~ ((a, b, a1) -> t) -> t
- Expected type: ((a, ((a, b, a1) -> t) -> t, ((a, b, a1) -> t) -> t)
- -> ((a, b, a1) -> t) -> t)
- -> a2
- Actual type: ((a, b, a1) -> ((a, b, a1) -> t) -> t) -> a2
- • In the first argument of ‘fight’, namely ‘robotA’
- In the expression: fight robotA robotB
- In an equation for ‘robotA'’: robotA' = fight robotA robotB
- • Relevant bindings include
- robotA' :: a1 (bound at robot.hs:48:10)
- robotB :: ((a3, ((a3, b1, a2) -> t1) -> t1,
- ((a3, b1, a2) -> t1) -> t1)
- -> ((a3, b1, a2) -> t1) -> t1)
- -> a1
- (bound at robot.hs:47:22)
- robotA :: ((a, b, a1) -> ((a, b, a1) -> t) -> t) -> a2
- (bound at robot.hs:47:15)
- fightOneRound :: (((a, b, a1) -> ((a, b, a1) -> t) -> t) -> a2)
- -> (((a3, ((a3, b1, a2) -> t1) -> t1, ((a3, b1, a2) -> t1) -> t1)
- -> ((a3, b1, a2) -> t1) -> t1)
- -> a1)
- -> (a1, a2)
- (bound at robot.hs:47:1)
- |
- 48 | where robotA' = fight robotA robotB
- | ^^^^^^
- robot.hs:48:33: error:
- • Occurs check: cannot construct the infinite type:
- a2 ~ ((a3, b1, a2) -> t1) -> t1
- Expected type: ((a3, b1, a2) -> ((a3, b1, a2) -> t1) -> t1) -> a1
- Actual type: ((a3, ((a3, b1, a2) -> t1) -> t1,
- ((a3, b1, a2) -> t1) -> t1)
- -> ((a3, b1, a2) -> t1) -> t1)
- -> a1
- • In the second argument of ‘fight’, namely ‘robotB’
- In the expression: fight robotA robotB
- In an equation for ‘robotA'’: robotA' = fight robotA robotB
- • Relevant bindings include
- robotB' :: a2 (bound at robot.hs:49:10)
- robotB :: ((a3, ((a3, b1, a2) -> t1) -> t1,
- ((a3, b1, a2) -> t1) -> t1)
- -> ((a3, b1, a2) -> t1) -> t1)
- -> a1
- (bound at robot.hs:47:22)
- robotA :: ((a, b, a1) -> ((a, b, a1) -> t) -> t) -> a2
- (bound at robot.hs:47:15)
- fightOneRound :: (((a, b, a1) -> ((a, b, a1) -> t) -> t) -> a2)
- -> (((a3, ((a3, b1, a2) -> t1) -> t1, ((a3, b1, a2) -> t1) -> t1)
- -> ((a3, b1, a2) -> t1) -> t1)
- -> a1)
- -> (a1, a2)
- (bound at robot.hs:47:1)
- |
- 48 | where robotA' = fight robotA robotB
- | ^^^^^^
- -}
- -- doesn't work
- -- fightNRounds 0 robotA robotB = (robotA, robotB)
- -- fightNRounds n robotA robotB = fightNRounds (n-1) robotA' robotB'
- -- where robotA' = fight robotA robotB
- -- robotB' = fight robotB robotA
Advertisement
Add Comment
Please, Sign In to add comment