用于组合两个函数f和g的Haskell表示法,其中g表示多个参数

dsp*_*pyz 8 haskell operators currying variadic-functions function-composition

我经常发现我想要编写两个函数f和g,但g需要多个参数.Haskell是否提供了一组操作符(我知道我可以自己编写它,但它看起来相当常见,我不想复制已经存在于Haskell中的运算符)

即类似的东西

(.@) = (.)
(.@@) f g x1 x2 = f $ g x1 x2
(.@@@) f g x1 x2 x3 = f $ g x1 x2 x3
(.@@@@) f g x1 x2 x3 x4 = f $ g x1 x2 x3 x4
...
Run Code Online (Sandbox Code Playgroud)

达到一些合理数量的论点

Oma*_*ena 6

我知道你得到了你想要的答案,但我想指出这些组合器具有以下可爱的实现方式:

(.:)   = (.) . (.)
(.:.)  = (.) . (.) . (.)
(.::)  = (.) . (.) . (.) . (.)
(.::.) = (.) . (.) . (.) . (.) . (.)
Run Code Online (Sandbox Code Playgroud)

如果您只需要完全应用它们:

f .: g   = (f .) . g
f .:. g  = ((f .) .) . g
f .:: g  = (((f .) .) .) . g
f .::. g = ((((f .) .) .) .) . g
Run Code Online (Sandbox Code Playgroud)

在不定义运算符的情况下直接使用这些表达式似乎并不那么糟糕.至少第一个,(f .) . g对我来说似乎足够可读.


dsp*_*pyz 5

从@bheklilr的评论来看,我正在寻找的答案在组合库中:http://hackage.haskell.org/package/composition-1.0.1.0/docs/Data-Composition.html

函数(.:),(.:.),(.::),(.::.)等正是我所想的