I think no one answered this very important question:
I want to ask the string str or not?
I will try to.
The variable type str is String , yes. However, the scope of this variable is very limited. I think that for understanding it is necessary to discourage o-notation:
read' = readFile "/home/shk/workspace/src/test.txt" >>= (\str -> putStrLn str)
I think it makes sense here why str not good enough. This is an argument to the function you pass >>= . Its value becomes available only when someone calls your function, which happens only when an IO action containing it is performed.
In addition, the type read' :: IO () is determined not so much by putStrLn str as by the return type of the >>= operator. Take a look at it (specializes in monad IO ):
(>>=) :: IO a -> (a -> IO b) -> IO b
You can see that the result is always the IO b action, so trying to change any of the arguments will not help.
You can read the monad tutorial if you want to understand why the type is what it is. The intuition is this: you cannot perform an action without performing an action.
And from the practical side of the question, using the value returned by some action, instead of trying to use (extractValue inputAction) , which makes no sense because extractValue not possible, try inputAction >>= use if your use includes I / O or fmap use inputAction if it is not.
Rotorsor
source share