Programming Praxis – Spectacular Seven

In today’s Programming Praxis exercise our task is to run a simulation of a ballgame to see if the scoring mechanic is fair. The provided Scheme solution clocks in at 25 lines. Let’s see if we can do any better.

First, some imports.

import Control.Applicative
import Control.Monad
import Data.List
import System.Random

After a match, the winner gets a point and the loser is moved to the end of the queue.

match :: Int -> [(a, Int)] -> Int -> [(a, Int)]
match ps ~(x:y:r) w = (p,s + if ps > 7 then 2 else 1) : r ++ [c]
    where ((p,s), c) = if w == 0 then (x,y) else (y,x)

A game ends when one of the teams has 7 or more points.

game :: IO Int
game = f 0 (zip [1..8] [0,0..]) . randomRs (0,1) <$> newStdGen
       where f ps a ~(x:xs) = maybe (f (ps+1) (match ps a x) xs) fst $
                              find ((>= 7) . snd) a

To simulate the game, we play a number of games and calculate the winning percentages of each team.

simulate :: Int -> IO [Float]
simulate n = (\ws -> map (\x -> 100 * (l x - 1) / l ws) . group .
                     sort $ ws ++ [1..8]) <$> replicateM n game
             where l = fromIntegral . length

All that’s left is to run the simulation.

main :: IO ()
main = mapM_ print =<< simulate 10000

That leaves us with 7 lines, more than a two thirds reduction compared to the Scheme solution. That’ll do nicely.

About these ads

Tags: , , , , , , , ,

One Response to “Programming Praxis – Spectacular Seven”

  1. Kwezan Says:

    Hi,
    It would be nice to see your “bonsai” solutions to “Modern Elliptic Curve Factorization Part II” and “Integer Factorization”
    excercises.

    Best regards,
    Kwezan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 35 other followers

%d bloggers like this: