diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2015-12-04 18:47:34 +0100 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2015-12-04 18:47:34 +0100 |
commit | 483112fdf522550e852c45a09e73fd3120b161be (patch) | |
tree | ac2094bf2d1d07b675d72721a4baadb006b45f70 | |
parent | 5d3e86e66309f66b9c40e97a8800313b59964e2f (diff) | |
download | uni-483112fdf522550e852c45a09e73fd3120b161be.tar uni-483112fdf522550e852c45a09e73fd3120b161be.tar.gz uni-483112fdf522550e852c45a09e73fd3120b161be.tar.bz2 uni-483112fdf522550e852c45a09e73fd3120b161be.tar.xz uni-483112fdf522550e852c45a09e73fd3120b161be.zip |
better cleanup
-rw-r--r-- | ws2015/ffp/blaetter/07/FFP_U07_Monaden2.hs | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/ws2015/ffp/blaetter/07/FFP_U07_Monaden2.hs b/ws2015/ffp/blaetter/07/FFP_U07_Monaden2.hs index 18eacfa..8e92dee 100644 --- a/ws2015/ffp/blaetter/07/FFP_U07_Monaden2.hs +++ b/ws2015/ffp/blaetter/07/FFP_U07_Monaden2.hs | |||
@@ -278,18 +278,19 @@ type MM = Map ([Monster], [Monster]) Double -- MM stands for MonsterManual | |||
278 | 278 | ||
279 | memoWinchance :: [Monster] -> [Monster] -> Double | 279 | memoWinchance :: [Monster] -> [Monster] -> Double |
280 | memoWinchance atts defs = runST (newSTRef Map.empty >>= memoWinchance' atts defs) | 280 | memoWinchance atts defs = runST (newSTRef Map.empty >>= memoWinchance' atts defs) |
281 | where | ||
282 | memoWinchance' [] _ _ = return 0 -- We do this here so we don't clutter our manual with entries for empty lists | ||
283 | memoWinchance' _ [] _ = return 1 | ||
284 | memoWinchance' atts defs mmRef = recall >>= fromMaybeM (winchance >>= memoize) | ||
285 | 281 | ||
282 | memoWinchance' :: forall s. [Monster] -> [Monster] -> STRef s MM -> ST s Double | ||
283 | memoWinchance' [] _ _ = return 0 -- We do this here so we don't clutter our manual with entries for empty lists | ||
284 | memoWinchance' _ [] _ = return 1 | ||
285 | memoWinchance' atts defs mmRef = recall >>= fromMaybeM (winchance >>= memoize) | ||
286 | where | ||
286 | recall = Map.lookup (atts, defs) <$> readSTRef mmRef | 287 | recall = Map.lookup (atts, defs) <$> readSTRef mmRef |
287 | memoize x = modifySTRef mmRef (Map.insert (atts, defs) x) >> return x | 288 | memoize x = modifySTRef mmRef (Map.insert (atts, defs) x) >> return x |
288 | winchance = winchance' atts defs mmRef | 289 | winchance = winchance' atts defs mmRef |
289 | |||
290 | fromMaybeM x Nothing = x | 290 | fromMaybeM x Nothing = x |
291 | fromMaybeM _ (Just x) = return x | 291 | fromMaybeM _ (Just x) = return x |
292 | 292 | ||
293 | winchance' :: forall s. [Monster] -> [Monster] -> STRef s MM -> ST s Double | ||
293 | winchance' allatts@(a:atts) alldefs@(d:defs) mmRef | 294 | winchance' allatts@(a:atts) alldefs@(d:defs) mmRef |
294 | | hitpoints a <= 0 = memoWinchance' atts alldefs mmRef | 295 | | hitpoints a <= 0 = memoWinchance' atts alldefs mmRef |
295 | | hitpoints d <= 0 = memoWinchance' allatts defs mmRef | 296 | | hitpoints d <= 0 = memoWinchance' allatts defs mmRef |