Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Greetings from /tg/

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 "-")

Name: Anonymous 2010-04-05 0:57

>>1
Example:
*Main> showHistogram (combineXdY 6 3)
| 3
--| 4
-----| 5
---------| 6
--------------| 7
--------------------| 8
------------------------| 9
--------------------------| 10
--------------------------| 11
------------------------| 12
--------------------| 13
--------------| 14
---------| 15
-----| 16
--| 17
| 18

Name: Anonymous 2010-04-05 1:19

>>1

-- Generalized probability fn.

pCompareHistogram i lge histogram = ((/) `on` fromInteger.sumFreqs) subseq histogram
    where subseq = op ((comp i).fst) histogram
          (op,comp) = case lge of
                           GT -> (dropWhile,(>))
                           LT -> (takeWhile,(>=))
                           EQ -> (filter,(==))

Name: Anonymous 2010-04-05 12:03

You can make it more elegant.

import Data.List (sort, group)
import Control.Arrow ((&&&))

throw = map sum . sequence . map (\n -> [1 .. n])
makeHist = map (head &&& length) . group . sort
showHist h = mapM_ showLine h
  where showLine (n, k) = putStrLn $ rjust (show n) ++ " : " ++ replicate k '='
        rjust s = replicate (m - length s) ' ' ++ s
        m = last $ sort $ map (length . show . fst) h

d = replicate
try = showHist . makeHist . throw

main = try $ 3`d`6

Name: 4 2010-04-05 12:11

Actually, you can replace sequence . map with mapM.

Name: Anonymous 2010-04-05 13:08

(car (cdr (eval (apply))))

Name: Anonymous 2010-04-05 13:13

>>6
[Anonymous@/prog/ ~]$ ikarus
Ikarus Scheme version 0.0.4-rc1+ (revision 1870, build 2010-03-23)
Copyright (c) 2006-2009 Abdulaziz Ghuloum

(apply)
Unhandled exception
 Condition components:
   1. &assertion
   2. &who: apply
   3. &message: "incorrect number of arguments"
   4. &irritants: (#<procedure apply> 0)

Name: Anonymous 2010-04-05 13:34

…………………...„„-~^^~„-„„_
………………„-^*'' : : „'' : : : : *-„
…………..„-* : : :„„--/ : : : : : : : '\
…………./ : : „-* . .| : : : : : : : : '|
……….../ : „-* . . . | : : : : : : : : |
………...\„-* . . . . .| : : : : : : : :'|
……….../ . . . . . . '| : : : : : : : :|
……..../ . . . . . . . .'\ : : : : : : : |
……../ . . . . . . . . . .\ : : : : : : :|
……./ . . . . . . . . . . . '\ : : : : : /
….../ . . . . . . . . . . . . . *-„„„„-*'
….'/ . . . . . . . . . . . . . . '|
…/ . . . . . . . ./ . . . . . . .|
../ . . . . . . . .'/ . . . . . . .'|
./ . . . . . . . . / . . . . . . .'|
'/ . . . . . . . . . . . . . . . .'|
'| . . . . . \ . . . . . . . . . .|
'| . . . . . . \„_^- „ . . . . .'|
'| . . . . . . . . .'\ .\ ./ '/ . |
| .\ . . . . . . . . . \ .'' / . '|
| . . . . . . . . . . / .'/ . . .|
| . . . . . . .| . . / ./ ./ . .|

Don't change these.
Name: Email:
Entire Thread Thread List