Created
March 6, 2023 09:32
-
-
Save takanuva/7b6caf386eb664e20d2049a6943f1fc3 to your computer and use it in GitHub Desktop.
Functional Programming - class 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- | |
IO operations can be composed through >>=, which is: | |
(>>=) :: IO a -> (a -> IO b) -> IO b | |
The other building block for IOs is the return operation: | |
return :: a -> IO a | |
-} | |
foo :: IO () | |
foo = | |
putStrLn "Type a number:" >>= (\() -> | |
getLine >>= (\x -> | |
putStrLn "Type another number:" >>= (\() -> | |
getLine >>= (\y -> | |
putStrLn (show (read x + read y)))))) | |
bar :: IO Integer | |
bar = do | |
putStrLn "Type a number:" | |
x <- getLine | |
putStrLn "Type another number:" | |
y <- getLine | |
let n = read x + read y | |
putStrLn $ show n | |
return n | |
baz :: IO () | |
baz = do | |
-- Call the action bar once, binding the result into n | |
n <- bar | |
-- Call it a second time, biding the result into m | |
m <- bar | |
-- We have asked the user for four numbers now, | |
-- So just sum them all up and show it to him! | |
putStrLn "Final result:" | |
putStrLn $ show (n + m) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment