是否有 C++'s std::bind2nd 的内置 Haskell 等效项?
我缺少的是部分应用函数的第二个参数而不是第一个参数的能力。 当我想将函数传递给诸如 map 之类的东西,但不必每次都为其编写 lambda 时,这尤其有用。
我为此编写了自己的函数(定义如下,以防万一确实没有任何内置函数并且其他人对此感到好奇),但我真的很想知道 Prelude 中是否已经存在此函数习语,因为我更喜欢重用而不是重新发明。
这是我的定义和一个简单的例子:
bind2nd :: (a -> b -> c) -> b -> a -> c
bind2nd f b = \a -> f a b
foo :: Int -> Bool -> String
foo n b | b = show n
| otherwise = "blabla"
alwaysN :: Int -> String
alwaysN = bind2nd foo True
What I'm missing is the ability to partially apply the second argument of a function rather than the first. This is especially useful when I want to pass the function to something like map, but without having to write a lambda for it each time.
I wrote my own function for this (definition below, just in case there indeed isn't any built-in function for this and anyone else was curious), but I would really like to know if there already exists something in the Prelude for this idiom as I prefer to reuse rather than reinvent.
Here is my definition and a trivial example:
bind2nd :: (a -> b -> c) -> b -> a -> c
bind2nd f b = \a -> f a b
foo :: Int -> Bool -> String
foo n b | b = show n
| otherwise = "blabla"
alwaysN :: Int -> String
alwaysN = bind2nd foo True
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它称为
flip
。示例:
为了将来参考,只需在 Hoogle 中搜索问题中的类型签名即可找到它,即
It's called
flip
.Example:
For future reference, it could have been found by simply searching Hoogle for the type signature in your question, namely (a -> b -> c) -> b -> a -> c. :-)