From 5d3e86e66309f66b9c40e97a8800313b59964e2f Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Fri, 4 Dec 2015 18:46:13 +0100 Subject: cleanup --- ws2015/ffp/blaetter/07/FFP_U07_Monaden2.hs | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'ws2015') diff --git a/ws2015/ffp/blaetter/07/FFP_U07_Monaden2.hs b/ws2015/ffp/blaetter/07/FFP_U07_Monaden2.hs index 5200851..18eacfa 100644 --- a/ws2015/ffp/blaetter/07/FFP_U07_Monaden2.hs +++ b/ws2015/ffp/blaetter/07/FFP_U07_Monaden2.hs @@ -278,28 +278,27 @@ type MM = Map ([Monster], [Monster]) Double -- MM stands for MonsterManual memoWinchance :: [Monster] -> [Monster] -> Double memoWinchance atts defs = runST (newSTRef Map.empty >>= memoWinchance' atts defs) - -memoWinchance' :: forall s. [Monster] -> [Monster] -> STRef s MM -> ST s Double -memoWinchance' [] _ _ = return 0 -- We do this here so we don't clutter our manual with entries for empty lists -memoWinchance' _ [] _ = return 1 -memoWinchance' atts defs mmRef = recall >>= fromMaybeM (winchance >>= memoize) where + memoWinchance' [] _ _ = return 0 -- We do this here so we don't clutter our manual with entries for empty lists + memoWinchance' _ [] _ = return 1 + memoWinchance' atts defs mmRef = recall >>= fromMaybeM (winchance >>= memoize) + recall = Map.lookup (atts, defs) <$> readSTRef mmRef memoize x = modifySTRef mmRef (Map.insert (atts, defs) x) >> return x winchance = winchance' atts defs mmRef + fromMaybeM x Nothing = x fromMaybeM _ (Just x) = return x -winchance' :: forall s. [Monster] -> [Monster] -> STRef s MM -> ST s Double -winchance' allatts@(a:atts) alldefs@(d:defs) mmRef - | hitpoints a <= 0 = memoWinchance' atts alldefs mmRef - | hitpoints d <= 0 = memoWinchance' allatts defs mmRef - | otherwise = do - let attchance = nextHitchance dice (strength a) (strength d) - defchance = 1 - attchance - atthit <- memoWinchance' allatts (takeHit d:defs) mmRef - defhit <- memoWinchance' (takeHit a:atts) alldefs mmRef - return $ (attchance * atthit) + (defchance * defhit) + winchance' allatts@(a:atts) alldefs@(d:defs) mmRef + | hitpoints a <= 0 = memoWinchance' atts alldefs mmRef + | hitpoints d <= 0 = memoWinchance' allatts defs mmRef + | otherwise = do + let attchance = nextHitchance dice (strength a) (strength d) + defchance = 1 - attchance + atthit <- memoWinchance' allatts (takeHit d:defs) mmRef + defhit <- memoWinchance' (takeHit a:atts) alldefs mmRef + return $ (attchance * atthit) + (defchance * defhit) -- -- Simples Beispiel zur Verwendung der ST-Monade: -- cgit v1.2.3