Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. import System.Random
  2. import Control.Monad.State
  3.  
  4. -- given a [Int] add random numbers to it
  5. a :: [Int]
  6. a = [3, 1, 6, 7, 9]
  7.  
  8. myFunc :: Int -> State StdGen Int
  9. myFunc x = do
  10. gen <- get
  11. let (y, gen2) = randomR (100,200) gen
  12. put gen2
  13. return $ y + x
  14.  
  15. myFunc2 :: Int -> State StdGen (Maybe Int)
  16. myFunc2 0 = return Nothing
  17. myFunc2 x = do
  18. gen <- get
  19. let (y, gen2) = randomR (100,200) gen
  20. put gen2
  21. return $ Just $ y*x
  22.  
  23. myFunc3 :: Int -> State StdGen (Maybe Int)
  24. myFunc3 0 = return Nothing
  25. myFunc3 x = do
  26. gen <- get
  27. let (y, gen2) = randomR (100,200) gen
  28. put gen2
  29. return $ Just $ y*x + 2
  30.  
  31. myConnector :: Maybe Int -> State StdGen (Maybe Int)
  32. myConnector Nothing = return Nothing
  33. myConnector (Just x) = myFunc3 x
  34.  
  35. x :: State StdGen [Int]
  36. x = (sequenceA $ myFunc <$> a)
  37.  
  38. y :: [Int] -> State StdGen [Maybe Int]
  39. y = (\x -> sequenceA $ myFunc2 <$> x )
  40.  
  41. z :: [Maybe Int] -> State StdGen ([Maybe Int])
  42. z = (\x -> sequenceA $ myConnector <$> x)
  43.  
  44. g = evalState (x >>= (z <=< y )) (mkStdGen 4567)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement