haskell中具有多值函数的功能组合? [英] Functional composition with multi-valued functions in haskell?
问题描述
我想知道是否有可能做功能组合,其功能需要多个参数。我希望能够做到这一点。
x =(+3)。(*)
code>
设置x等于一个函数,它将两个数字的乘积加上三。
有很多方法可以做到这一点,但它们都有些尴尬。
((+ 3)。)。 (*)
≡fmap(+3)。 (*)
≡咖喱$(+3)。 uncurry(*)
≡\l r - > l * r + 3
哦,等等,这是签名,其中还有一个紧凑的定义,猜测($。)。(。))(+3)(*)
我认为最明确的lambda解决方案在这里是最好的。
有什么帮助,而且通常在本地作为一个(或两个)内行来完成,就是将这个组合定义为一个自定义中缀:
(。:) ::(c-> d) - > (a-> b-> c)→> a-> b-> d
f。:i = \\\ r - > f $ ilr
它允许您简单地写(+ 3)。: (*)
。
BTW,对于类似的
(b-> b-> c) - > ; (a-> b) - > a-> a-> c
(将正确的函数预先分配给中缀的两个参数)存在一个广泛使用的标准实现。 I was wondering if it was possible to do functional composition with functions that take more than one argument. I want to be able to do something like this
x = (+3).(*)
setting x equal to a function that adds three to the product of two numbers.
There are multiple ways to do it, but they're all somewhat awkward.
((+3).) . (*)
≡ fmap (+3) . (*)
≡ curry $ (+3) . uncurry (*)
≡ \l r -> l*r + 3
Oh, wait, this was the signature where there's also a compact definition, guess what it's called...
((.).(.)) (+3) (*)
I'd argue that the lambda solution, being most explicit, is rather the best here.
What helps, and is often done just locally as a one(or two)-liner, is to define this composition as a custom infix:
(.:) :: (c->d) -> (a->b->c) -> a->b->d
f .: i = \l r -> f $ i l r
Which allows you to write simply (+3) .: (*)
.
BTW, for the similar (b->b->c) -> (a->b) -> a->a->c
(precompose the right function to both arguments of the infix) there exists a widely-used standard implementation.
这篇关于haskell中具有多值函数的功能组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!