gpt4 book ai didi

haskell - 是否有像 `>>` 这样的标准函数,但返回第一个操作数的结果?

转载 作者:行者123 更新时间:2023-12-02 04:07:55 24 4
gpt4 key购买 nike

例如,假设我想读一行并按响铃:

λ getLine >> putChar '\007'
How lang and dreary is the night when I am frae my Dearie.
-- Blip and `()`. The line is lost.
λ getLine >>= (\x -> putChar '\007' >> 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 中的其他函数轻松构造。

最佳答案

关于haskell - 是否有像 `>>` 这样的标准函数,但返回第一个操作数的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51478836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com