Guest User

Untitled

a guest
Mar 22nd, 2021
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. robot (name, attack, hp) = \message -> message (name, attack, hp)
  2.  
  3. name (n, _, _) = n
  4.  
  5. attack (_, a, _) = a
  6.  
  7. hp (_, _, hp) = hp
  8.  
  9. getName aRobot = aRobot name
  10.  
  11. setName aRobot newName = aRobot (\(n, a, h) -> robot (newName, a, h))
  12.  
  13. getAttack aRobot = aRobot attack
  14. setAttack aRobot newAttack = aRobot (\(n, a, h) -> robot (n, newAttack, h))
  15.  
  16. getHP aRobot = aRobot hp
  17. setHP aRobot newHP = aRobot (\(n, a, h) -> robot (n, a, newHP))
  18.  
  19. printRobot aRobot = aRobot (\(name, attack, hp) -> name ++ " attack:" ++ (show attack) ++ " hp:" ++ (show hp))
  20.  
  21. damage aRobot attackDamage =
  22.   aRobot (\(n, a, h) -> robot (n, a, h - attackDamage))
  23.  
  24. fight aRobot defender = damage defender attack
  25.   where attack = if getHP aRobot > 10
  26.                  then getAttack aRobot
  27.                  else 0
  28.  
  29. killerRobot = robot ("Kill3r", 25, 200)
  30. gentleGiant = robot ("Mr. Friendly", 10, 300)
  31.  
  32. gentleGiantRound1 = fight killerRobot gentleGiant
  33. killerRobotRound1 = fight gentleGiant killerRobot
  34.  
  35. gentleGiantRound2 = fight killerRobotRound1 gentleGiantRound1
  36. killerRobotRound2 = fight gentleGiantRound1 killerRobotRound1
  37.  
  38. gentleGiantRound3 = fight killerRobotRound2 gentleGiantRound2
  39. killerRobotRound3 = fight gentleGiantRound2 killerRobotRound2
  40.  
  41. -- doesn't work as expected, cannot print the resulting robot
  42. getWinner aRobot bRobot = if ahp > bhp then aRobot else bRobot
  43.   where bhp = getHP bRobot
  44.         ahp = getHP aRobot
  45.  
  46. -- doesn't work compile at all
  47. fightOneRound robotA robotB = (robotA', robotB')
  48.    where robotA' = fight robotA robotB
  49.         robotB' = fight robotB robotA
  50.  
  51. {-
  52. robot.hs:48:26: error:
  53.     • Occurs check: cannot construct the infinite type:
  54.         a1 ~ ((a, b, a1) -> t) -> t
  55.       Expected type: ((a, ((a, b, a1) -> t) -> t, ((a, b, a1) -> t) -> t)
  56.                       -> ((a, b, a1) -> t) -> t)
  57.                      -> a2
  58.         Actual type: ((a, b, a1) -> ((a, b, a1) -> t) -> t) -> a2
  59.     • In the first argument of ‘fight’, namely ‘robotA’
  60.       In the expression: fight robotA robotB
  61.       In an equation for ‘robotA'’: robotA' = fight robotA robotB
  62.     • Relevant bindings include
  63.         robotA' :: a1 (bound at robot.hs:48:10)
  64.         robotB :: ((a3, ((a3, b1, a2) -> t1) -> t1,
  65.                     ((a3, b1, a2) -> t1) -> t1)
  66.                    -> ((a3, b1, a2) -> t1) -> t1)
  67.                   -> a1
  68.           (bound at robot.hs:47:22)
  69.         robotA :: ((a, b, a1) -> ((a, b, a1) -> t) -> t) -> a2
  70.           (bound at robot.hs:47:15)
  71.         fightOneRound :: (((a, b, a1) -> ((a, b, a1) -> t) -> t) -> a2)
  72.                          -> (((a3, ((a3, b1, a2) -> t1) -> t1, ((a3, b1, a2) -> t1) -> t1)
  73.                               -> ((a3, b1, a2) -> t1) -> t1)
  74.                              -> a1)
  75.                          -> (a1, a2)
  76.           (bound at robot.hs:47:1)
  77.    |
  78. 48 |    where robotA' = fight robotA robotB
  79.    |                          ^^^^^^
  80.  
  81. robot.hs:48:33: error:
  82.     • Occurs check: cannot construct the infinite type:
  83.         a2 ~ ((a3, b1, a2) -> t1) -> t1
  84.       Expected type: ((a3, b1, a2) -> ((a3, b1, a2) -> t1) -> t1) -> a1
  85.         Actual type: ((a3, ((a3, b1, a2) -> t1) -> t1,
  86.                        ((a3, b1, a2) -> t1) -> t1)
  87.                       -> ((a3, b1, a2) -> t1) -> t1)
  88.                      -> a1
  89.     • In the second argument of ‘fight’, namely ‘robotB’
  90.       In the expression: fight robotA robotB
  91.       In an equation for ‘robotA'’: robotA' = fight robotA robotB
  92.     • Relevant bindings include
  93.         robotB' :: a2 (bound at robot.hs:49:10)
  94.         robotB :: ((a3, ((a3, b1, a2) -> t1) -> t1,
  95.                     ((a3, b1, a2) -> t1) -> t1)
  96.                    -> ((a3, b1, a2) -> t1) -> t1)
  97.                   -> a1
  98.           (bound at robot.hs:47:22)
  99.         robotA :: ((a, b, a1) -> ((a, b, a1) -> t) -> t) -> a2
  100.           (bound at robot.hs:47:15)
  101.         fightOneRound :: (((a, b, a1) -> ((a, b, a1) -> t) -> t) -> a2)
  102.                          -> (((a3, ((a3, b1, a2) -> t1) -> t1, ((a3, b1, a2) -> t1) -> t1)
  103.                               -> ((a3, b1, a2) -> t1) -> t1)
  104.                              -> a1)
  105.                          -> (a1, a2)
  106.           (bound at robot.hs:47:1)
  107.    |
  108. 48 |    where robotA' = fight robotA robotB
  109.    |                                 ^^^^^^
  110. -}
  111.  
  112. -- doesn't work
  113. -- fightNRounds 0 robotA robotB = (robotA, robotB)
  114. -- fightNRounds n robotA robotB = fightNRounds (n-1) robotA' robotB'
  115. --   where robotA' = fight robotA robotB
  116. --         robotB' = fight robotB robotA
Advertisement
Add Comment
Please, Sign In to add comment