Sleeping Beauty problem: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (→{{header|Raku}}: Add a Raku example) |
(→{{header|Python}}: Added a functionally composed variant.) |
||
Line 146: | Line 146: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Procedural=== |
|||
<lang python>from random import choice |
<lang python>from random import choice |
||
Line 184: | Line 185: | ||
Results of experiment: Sleeping Beauty should estimate a credence of: 0.333542254953276 |
Results of experiment: Sleeping Beauty should estimate a credence of: 0.333542254953276 |
||
</pre> |
</pre> |
||
===Functional=== |
|||
<lang python>'''Sleeping Beauty Problem''' |
|||
from random import choice |
|||
from itertools import repeat |
|||
from functools import reduce |
|||
# experiment :: IO Int -> (Int, Int) -> IO (Int, Int) |
|||
def experiment(headsWakings): |
|||
'''A pair of counts updated by a coin flip. |
|||
''' |
|||
heads, wakings = headsWakings |
|||
return ( |
|||
1 + heads, 1 + wakings |
|||
) if "h" == choice(["h", "t"]) else ( |
|||
heads, 2 + wakings |
|||
) |
|||
# ------------------------- TEST ------------------------- |
|||
# main :: IO () |
|||
def main(): |
|||
'''Observed results from one million runs.''' |
|||
n = 1_000_000 |
|||
heads, wakes = applyN(n)( |
|||
experiment |
|||
)( |
|||
(0, 0) |
|||
) |
|||
print( |
|||
f'{wakes} wakenings over {n} experiments.\n' |
|||
) |
|||
print('Sleeping Beauty should estimate credence') |
|||
print(f'at around {round(heads/wakes, 3)}') |
|||
# ----------------------- GENERIC ------------------------ |
|||
# applyN :: Int -> (a -> a) -> a -> a |
|||
def applyN(n): |
|||
'''n applications of f. |
|||
(Church numeral n). |
|||
''' |
|||
def go(f): |
|||
def ga(a, g): |
|||
return g(a) |
|||
def fn(x): |
|||
return reduce(ga, repeat(f, n), x) |
|||
return fn |
|||
return go |
|||
# MAIN --- |
|||
if __name__ == '__main__': |
|||
main()</lang> |
|||
{{Out}} |
|||
<pre>1500188 wakenings over 1000000 experiments. |
|||
Sleeping Beauty should estimate credence |
|||
at around 0.333</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |