Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

haskell - Convert a "do" notation with more than two actions to use the bind function

I know that the following "do" notation's "bind" function is equivalent to getLine >>= line -> putStrLn

do line <- getLine
   putStrLn line

But how is the following notation equivalent to bind function?

do line1 <- getLine
   putStrLn "enter second line"
   line2 <- getLine
   return (line1,line2)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I take it you are trying to see how to bind the result of "putStrLn". The answer is in the type of putStrLn:

putStrLn :: String -> IO ()

Remember that "()" is the unit type, which has a single value (also written "()"). So you can bind this in exactly the same way. But since you don't use it you bind it to a "don't care" value:

getLine >>= line1 ->
putStrLn "enter second line" >>= \_ ->
getline >>= line2 ->
return (line1, line2)

As it happens, there is an operator already defined for ignoring the return value, ">>". So you could just rewrite this as

getLine >>= line1 ->
putStrLn "enter second line" >>
getline >>= line2 ->
return (line1, line2)

I'm not sure if you are also trying to understand how bind operators are daisy-chained. To see this, let me put the implicit brackets and extra indentation in the example above:

getLine >>= (line1 ->
   putStrLn "enter second line" >> (
      getline >>= (line2 ->
         return (line1, line2))))

Each bind operator links the value to the left with a function to the right. That function consists of all the rest of the lines in the "do" clause. So the variable being bound through the lambda ("line1" in the first line) is in scope for the whole of the rest of the clause.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...