Name: Anonymous 2010-04-05 0:54
Hi there /prague/. I wrote a few simple functions to visualize and calculate probabilities of dice. Though you might be interested; maybe you can tell me how I'm ``doing it wrong''
.import Data.Function
import Data.List
add1dY y = concatMap (\x-> map (+x) [1..y])
addXdY y 0 xDy = xDy
addXdY y x xDy = addXdY y (x-1) (add1dY y xDy)
combineXdY y x = toHistogram $ addXdY y (x-1) [1..y]
sumFreqs = foldr ((+).snd) 0
pGreaterHist histogram i = ((/) `on` fromInteger.sumFreqs) greaters histogram
where greaters = dropWhile ((<= i).fst) histogram
pLessHist histogram i = ((/) `on` fromInteger.sumFreqs) lessers histogram
where lessers = takeWhile ((< i).fst) histogram
toHistogram [] = []
toHistogram lst =
toHistogram' (x,1) xs
where (x:xs) = sort lst
toHistogram' pair [] = [pair]
toHistogram' (x1,n) (x2:xs) =
if x1 == x2
then toHistogram' (x1,n+1) xs
else (x1,n) : (toHistogram' (x2,1) xs)
showHistogram :: [(Int, Int)] -> IO ()
showHistogram = foldr (\ (i,n) y -> (putStrLn ((reverse $ take n bars) ++ " " ++ (show i))) >> y) (return ())
where bars = "|"++(cycle "-")