是否有像`>>`这样的标准函数,但返回第一个操作数的结果?
Is there a standard function like `>>`, but returning the result of the first operand?
例如,假设我想读一行并按铃:
λ getLine >> putChar '[=10=]7'
How lang and dreary is the night when I am frae my Dearie.
-- Blip and `()`. The line is lost.
λ getLine >>= (\x -> putChar '[=10=]7' >> return x
I restless lie frae e'en to morn though I were ne'er sae weary.
"I restless lie frae e'en to morn though I were ne'er sae weary."
-- A line and also a blip side effect.
这个想法似乎与const
有很多共同点,唯一的区别是给出的值是有效的,并且都被执行了,尽管只保留了第一个动作的值。 (与 >>
不同,它保留了第二个的值。) 我的意思是:
λ constM a b = a >>= \x -> b >> return x
这是一个更复杂的示例,涉及来自 Text.ParserCombinators.ReadP
:
的解析器
λ readP_to_S (many1 (munch1 (not . isSpace) `constM` skipSpaces ) `constM` eof) <$> getLine
How slow ye move, ye heavy hours.
[(["How","slow","ye","move,","ye","heavy","hours."],"")]
我想知道这个函数是否在 base
中可用,或者是否可以从 base
中的其他函数轻松构造。
例如,假设我想读一行并按铃:
λ getLine >> putChar '[=10=]7'
How lang and dreary is the night when I am frae my Dearie.
-- Blip and `()`. The line is lost.
λ getLine >>= (\x -> putChar '[=10=]7' >> return x
I restless lie frae e'en to morn though I were ne'er sae weary.
"I restless lie frae e'en to morn though I were ne'er sae weary."
-- A line and also a blip side effect.
这个想法似乎与const
有很多共同点,唯一的区别是给出的值是有效的,并且都被执行了,尽管只保留了第一个动作的值。 (与 >>
不同,它保留了第二个的值。) 我的意思是:
λ constM a b = a >>= \x -> b >> return x
这是一个更复杂的示例,涉及来自 Text.ParserCombinators.ReadP
:
λ readP_to_S (many1 (munch1 (not . isSpace) `constM` skipSpaces ) `constM` eof) <$> getLine
How slow ye move, ye heavy hours.
[(["How","slow","ye","move,","ye","heavy","hours."],"")]
我想知道这个函数是否在 base
中可用,或者是否可以从 base
中的其他函数轻松构造。