hpastetwo
recent
new
New Revision
module LetsMakeADeal where {- Fairly naive attack on http://programmingpraxis.com/2009/07/24/lets-make-a-deal/ > ghci LetsMakeADeal.hs *LetsMakeADeal> rounds 100000 0.66252 -} import Control.Monad import Data.List import System.Random -- Pick a random element from a list. Maybe there's a nicer way? chooseFrom :: [a] -> IO a chooseFrom xs = do which <- getStdRandom $ randomR (0, length xs-1) return $ xs !! which -- Play one round of the game. Returns True if player should switch. play :: IO Bool play = do car <- chooseFrom doors -- Place the car. guess <- chooseFrom doors -- Player makes a first guess. -- Show them a goat. It can't be behind the door hiding the -- car, and it can't be the door they picked. shown <- chooseFrom $ delete guess $ delete car doors -- Player should switch if the other (non-guessed, -- non-shown) door is the right one. return $ car == head (delete guess $ delete shown doors) where doors = [0,1,2] -- Play n rounds of the game and work out how many times switching was -- the right thing to do. rounds :: Int -> IO Double rounds n = do outcomes <- replicateM n play let shouldHave = filter id outcomes return (fromIntegral (length shouldHave) / fromIntegral n)
author
title
language
ActionScript
ActionScript 3
ApacheConf
AppleScript
BBCode
Bash
Batchfile
Befunge
Boo
Brainfuck
C
C#
C++
CSS
CSS+Django/Jinja
CSS+Genshi Text
CSS+Mako
CSS+Myghty
CSS+PHP
CSS+Ruby
CSS+Smarty
Cheetah
Clojure
Common Lisp
D
Darcs Patch
Debian Control file
Debian Sourcelist
Delphi
Diff
Django/Jinja
Dylan
ERB
Erlang
Fortran
GAS
Genshi
Genshi Text
Gettext Catalog
Gnuplot
Groff
HTML
HTML+Cheetah
HTML+Django/Jinja
HTML+Genshi
HTML+Mako
HTML+Myghty
HTML+PHP
HTML+Smarty
Haskell
INI
IRC logs
Io
Java
Java Server Page
JavaScript
JavaScript+Cheetah
JavaScript+Django/Jinja
JavaScript+Genshi Text
JavaScript+Mako
JavaScript+Myghty
JavaScript+PHP
JavaScript+Ruby
JavaScript+Smarty
LLVM
Lighttpd configuration file
Literate Haskell
Logtalk
Lua
MOOCode
Makefile
Makefile
Mako
Matlab
Matlab session
MiniD
MoinMoin/Trac Wiki markup
MuPAD
MySQL
Myghty
NASM
Nginx configuration file
NumPy
OCaml
Objective-C
PHP
POVRay
Perl
Python
Python 3
Python 3.0 Traceback
Python Traceback
Python console session
RHTML
Raw token data
Redcode
Ruby
Ruby irb session
S
SQL
Scala
Scheme
Smalltalk
Smarty
SquidConf
Tcl
Tcsh
TeX
Text only
VB.net
VimL
XML
XML+Cheetah
XML+Django/Jinja
XML+Mako
XML+Myghty
XML+PHP
XML+Ruby
XML+Smarty
XSLT
YAML
c-objdump
cpp-objdump
d-objdump
objdump
reStructuredText
sqlite3con
channel
none