Guest User

Untitled

a guest
Jan 23rd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import System.Random
  3.  
  4.  
  5. data Direction = Up | Down deriving (Eq)
  6.  
  7.  
  8. main :: IO ()
  9. main = do
  10.   let layers = 4
  11.       tries = 10000000
  12.  
  13.   rs <- (getStdGen >>= (\g -> return $ randomRs (1,10) g)) :: IO [Int]
  14.      
  15.   let through = main' rs layers tries 0
  16.  putStrLn $ "Through: " ++ show ( fromIntegral through / fromIntegral tries )
  17.  
  18.  
  19. main'                      :: (Integral a) => [a] -> a -> a -> a -> a
  20. main' rs layers tries through
  21.  | tries == 0 = through
  22.  | otherwise =
  23.       if (movePhoton rs layers $ (0, Down))
  24.          then main' (tail rs) layers (tries - 1) (through + 1)
  25.           else main' (tail rs) layers (tries - 1) through
  26.  
  27.  
  28. movePhoton               :: (Integral a) => [a] -> a -> (a, Direction) -> Bool
  29. movePhoton rs layers photon =
  30.  let position   = fst photon
  31.      direction  = snd photon
  32.  in
  33.  
  34.    if head rs == 1 then -- reflection
  35.         if position == 0 && direction == Down
  36.            then False
  37.         else
  38.            if direction == Up
  39.               then movePhoton (tail rs) layers (position + 1, Down)
  40.               else movePhoton (tail rs) layers (position - 1, Up)
  41.          
  42.       else -- pass through
  43.         if position == 0 && direction == Up
  44.            then False
  45.         else if position == layers - 1 && direction == Down
  46.            then True
  47.         else
  48.            if direction == Up
  49.               then movePhoton (tail rs) layers (position - 1, Up)
  50.               else movePhoton (tail rs) layers (position + 1, Down)
Add Comment
Please, Sign In to add comment