Advertisement
Arcaedox

Father and Son Wallet example

Apr 12th, 2015
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import FRP.Elerea.Param
  2. import Control.Monad
  3.  
  4. {-
  5. This program is an example of functional reactive programming. There are two people a father and a son
  6. they each have an amount of money and initially each of their values is 0 and 10. However , The father always has £10
  7. more than his son. An updater signal keep track and tells when an amount of money has been added or removed from the
  8. amounts of the father and the son. Here is the code written with the FRP library Elerea.
  9. -}
  10.  
  11. main :: IO ()
  12. main  = do
  13.     smp <- start $ do
  14.         b <- stateful 0 (+)
  15.         d <- transfer 10 (\d b x -> b + 10) b
  16.         g <- input
  17.         gs <- effectful1 (inputCheck) g
  18.         bs <- effectful2 (\b d -> (shout b d)) b d
  19.         return g
  20.  
  21.     res <- forM [1,0,1,(-1),1,1,0, 6] smp
  22.     return ()
  23.  
  24. shout :: Int -> Int -> IO ()
  25. shout b d = putStrLn $ "The Boy has £" ++ show b ++ "\n" ++ "The Dad has £" ++ show d ++ "\n"
  26.  
  27. inputCheck :: Int -> IO ()
  28. inputCheck x
  29.     | x == 6 = return ()
  30.     | x > 0  = putStrLn $ "--Increased by £" ++ show x
  31.     | x < 0  = putStrLn $ "* Decreased by £" ++ show (abs x)
  32.     | x == 0 = putStrLn "..No Change"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement