By using (>>=), we can use the
result of first IO action in second IO action.
Prelude> :t (>>=) (>>=) :: Monad m => m a -> (a -> m b) -> m b
Sample.hs
square :: Integer -> Integer square x = (x*x) main = putStrLn "Enter a number" >> readLn >>= \x -> putStrLn ("Square of " ++ (show x) ++ " is " ++ show (square x))
$ ghc Sample.hs [1 of 1] Compiling Main ( Sample.hs, Sample.o ) Linking Sample ... $ $ ./Sample Enter a number 10 Square of 10 is 100
All the statements in the do block are
internally combined using >>, >>=.
Sample.hs
main = do putStrLn "'Enter a Number" a <- readLn print (a==10)
Above code snippet is translated like
below.
Sample.hs
main = do putStrLn "'Enter a Number" >> readLn >>= \a -> print (a==10)
No comments:
Post a Comment