Skip to content

Instantly share code, notes, and snippets.

@takanuva
Created March 6, 2023 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takanuva/7b6caf386eb664e20d2049a6943f1fc3 to your computer and use it in GitHub Desktop.
Save takanuva/7b6caf386eb664e20d2049a6943f1fc3 to your computer and use it in GitHub Desktop.
Functional Programming - class 8
{-
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