Guest User

Untitled

a guest
Jan 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import System.Random
  2.  
  3.  
  4. data Direction = Up | Down deriving (Eq)
  5.  
  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 :: Double )
  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 []     _      _                     = True
  30. movePhoton (r:rs) layers (position, direction)
  31.  | r == 1 && position == 0          && direction == Down = False
  32.  | r /= 1 && position == 0          && direction == Up   = False
  33.  | r /= 1 && position == layers - 1 && direction == Down = True
  34.  
  35.  -- reflection
  36.  | r == 1 = if direction == Up
  37.                     then movePhoton rs layers (position + 1, Down)
  38.                     else movePhoton rs layers (position - 1, Up)
  39.  -- pass through
  40.  | otherwise = if direction == Up
  41.                   then movePhoton rs layers (position - 1, Up)
  42.                   else movePhoton rs layers (position + 1, Down)
Add Comment
Please, Sign In to add comment