简体   繁体   English

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

[英]Haskell notation for composing two functions f and g where g takes multiple arguments

Often I find I want to compose two functions f and g, but g takes multiple arguments. 我经常发现我想要编写两个函数f和g,但g需要多个参数。 Does Haskell provide a set of operators for that (I know I could write it myself, but it seems fairly common and I don't want to duplicate an operator that already exists in Haskell) Haskell是否提供了一组操作符(我知道我可以自己编写它,但它看起来相当常见,我不想复制已存在于Haskell中的运算符)

ie something like 即类似的东西

(.@) = (.)
(.@@) 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
...

up to some reasonable number of arguments 达到一些合理数量的论点

I know you got the answer you wanted, but I wanted to point out that these combinators have the following cute implementation: 我知道你得到了你想要的答案,但我想指出这些组合器具有以下可爱的实现方式:

(.:)   = (.) . (.)
(.:.)  = (.) . (.) . (.)
(.::)  = (.) . (.) . (.) . (.)
(.::.) = (.) . (.) . (.) . (.) . (.)

and if you only need them fully applied: 如果您只需要完全应用它们:

f .: g   = (f .) . g
f .:. g  = ((f .) .) . g
f .:: g  = (((f .) .) .) . g
f .::. g = ((((f .) .) .) .) . g

It doesn't seem so terrible to use these expressions directly, without defining an operator. 在不定义运算符的情况下直接使用这些表达式似乎并不那么糟糕。 At least the first one, (f .) . g 至少第一个, (f .) . g (f .) . g , seems readable enough to me. (f .) . g ,对我来说似乎足够可读。

From @bheklilr 's comment, the answer I was looking for is in the composition library: http://hackage.haskell.org/package/composition-1.0.1.0/docs/Data-Composition.html 从@bheklilr的评论来看,我正在寻找的答案在组合库中: http//hackage.haskell.org/package/composition-1.0.1.0/docs/Data-Composition.html

The functions (.:), (.:.), (.::), (.::.) etc. do exactly what I was thinking 函数(。:),(。:。),(。::),(。::。)等正是我所想的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM