From: Ramakrishnan Muthukrishnan Date: Mon, 29 Dec 2014 15:47:40 +0000 (+0530) Subject: hw12: exercise 4 X-Git-Url: https://git.rkrishnan.org/listings/vdrive/rgr-080307.php?a=commitdiff_plain;p=yorgey.git hw12: exercise 4 --- diff --git a/hw12/Risk.hs b/hw12/Risk.hs index 24acbc0..31298fa 100644 --- a/hw12/Risk.hs +++ b/hw12/Risk.hs @@ -49,8 +49,15 @@ battle (Battlefield {attackers = a, defenders = d}) = endgame :: Battlefield -> Bool endgame (Battlefield {attackers=a, defenders=d}) = d == 0 || a < 2 +enemyDestroyed :: Battlefield -> Bool +enemyDestroyed bf = endgame bf && (defenders bf) == 1 + invade :: Battlefield -> Rand StdGen Battlefield -- invade battlefield = foldM (\acc f -> f $ acc) battlefield (repeat battle) invade battlefield = battle battlefield >>= \bf -> if (endgame bf) then return bf else invade bf +-- exercise 4 +successProb :: Battlefield -> Rand StdGen Double +successProb battlefield = (pure (filter enemyDestroyed) <*> replicateM 1000 (invade battlefield)) + >>= \xs -> return (fromIntegral (length xs) / 1000)