简体   繁体   English

涉及函数组合的常见模式(\ a b - > f(g a)(g b))

[英]A common pattern involving composition of functions (\a b -> f (g a) (g b))

The composition of f and g that looks like f和g的组成看起来像

f :. g = \a b -> f (g a) (g b)

is a pattern I find very often in my code. 是我在代码中经常发现的模式。 It is similar to unary function composition, only f is binary and I want g applied to both arguments before they are passed to f . 它类似于一元函数组合,只有f是二元的,我想在传递给f之前将g应用于两个参数。

When I ask lambdabot to convert this to point-free form, I get the weird incantation 当我要求lambdabot将其转换为无点形式时,我得到了奇怪的咒语

flip ((.) . f . g) g

which I'd rather not have in my code, so I end up just writing out the pattern explicitly. 在我的代码中我不想拥有它,所以我最终只是明确地写出了模式。

Is there a commonly accepted way of writing a combinator for this situation? 是否有一种普遍接受的方法来为这种情况编写组合子? Or am I weird for finding myself in this situation quite a lot? 或者我在这种情况下发现自己很奇怪?

I don't have an actual example of when I use this on hand right now since I have never thought to ask here when I've needed it, but one could imagine writing the euclidean distance formula very neatly with it, like so: 我现在没有一个实际的例子,因为当我需要时,我从未想过要问这里,但可以想象用它非常整齐地写出欧几里德距离公式,就像这样:

distance = sqrt . (+) :. (^2)

This function is called on in the Data.Function module. 该功能被称为on的在Data.Function模块。

It's often used infix, such as sqrt . (+) `on` (^2) 它经常使用中缀,例如sqrt . (+) `on` (^2) sqrt . (+) `on` (^2) . sqrt . (+) `on` (^2)

暂无
暂无

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

相关问题 f, g, h :: Kleisli ((-&gt;) e) ab &lt;=&gt; f &gt;&gt;&gt; (g &amp;&amp;&amp; h) = (f &gt;&gt;&gt; g) &amp;&amp;&amp; (f &gt;&gt;&gt; h)? - f, g, h :: Kleisli ((->) e) a b <=> f >>> (g &&& h) = (f >>> g) &&& (f >>> h)? 找到Haskell函数f,g使得fg = f。 G - Find Haskell functions f, g such that f g = f . g 模式中的解析错误:f。 g在fmap中(f.g)= fmap f。 fmap g - Parse error in pattern: f . g in fmap (f . g) = fmap f . fmap g Haskell-如何使用(。)fg编写两次函数-函数组成 - Haskell - How to write twice function using (.) f g - function composition fgx的类型= g。 GX - Type of f g x = g . g x 给定函数g :: a - &gt; b - &gt; ... - &gt; z,我怎样才能像h ::(a,b,...) - &gt; z一样使用它? - Given a function g :: a -> b -> … -> z, how can I use it like h :: (a, b, …) -> z? ((fa)b)与Haskell中的(fab)相同吗? - Is ((f a) b) the same as (f a b) in Haskell? 是否有类型签名的已知组合函数(a - &gt; b) - &gt;(b - &gt; c) - &gt;(a - &gt;(b,c))? - Is there a known composition function for the type signature (a -> b) -> (b -> c) -> (a -> (b, c))? 用于组合两个函数f和g的Haskell表示法,其中g表示多个参数 - Haskell notation for composing two functions f and g where g takes multiple arguments Functor用于(a - &gt; b) - &gt;(fa - &gt; fb),什么是(类别c)=&gt; cab - &gt; c(fa)(fb)? - Functor is for (a -> b) -> (f a -> f b), what is for (Category c) => c a b -> c (f a) (f b)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM