Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import Data.Map as M
  2. import Data.Maybe
  3. import Numeric
  4.  
  5. learn :: [Bool] -> Map [Bool] Double -> Map [Bool] Double
  6. learn hist brain = Prelude.foldr learn' brain [3..5]
  7. where learn' n = M.insertWithKey (const (+)) (take n hist) 1.0
  8.  
  9. guess :: Map [Bool] Double -> [Bool] -> Bool
  10. guess brain hist = wAvg (Prelude.map guess' [2..4]) >= 0.5
  11. where wAvg xs = sum (Prelude.map (uncurry (*)) xs) / sum (Prelude.map snd xs)
  12. guess' n = (occ True / (occ True + occ False), occ True + occ False)
  13. where occ val = fromMaybe 0.0 $ M.lookup (val : take n hist) brain
  14.  
  15. play :: Int -> (Double, Double) -> [Bool] -> Map [Bool] Double -> IO a
  16. play turn (wons, total) hist brain = do
  17. press <- getLine
  18. let key = press == "f"
  19. let right = key == guess brain hist
  20. let wins = (if right then succ else id) wons
  21. print $ "I guessed " ++ (if right then "RIGHT!" else "wrong.")
  22. ++ " My avg: " ++ showFFloat (Just 2) (wins / succ total) ""
  23. play (succ turn) (wins, succ total) (key:hist) (learn (key:hist) brain)
  24.  
  25. main = do
  26. print "aaronson oracle | Press 'f' or 'd' over and over (followed by enter)"
  27. print " | and we'll try to predict which you'll press next."
  28. play 0 (0.0, 0.0) [] M.empty
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement