本文介绍了与二元运算符组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一件相当简单的事情;
我正在使用运算符(++)和Data.Map insertWith ,它工作正常,但我想消除创建的值中的重复项,所以想要使用nub进行组合。



我尝试过(nub(++)),(nub $(++)),(nub。(++)),都无济于事, (++)的类型与预期的nub类型([a])不匹配。



我当然可以定义一个辅助函数或lambda,但是我认为可能有一个更清晰的组合。



提示请!

解决方案

你可以写成:

 ((nub。)。)(++)

示例:

  Prelude Data.List> ((nub。)。)(++)[1,2,3] [3,4,5] 
[1,2,3,4,5]

一般而言,您有

 (f )gx = f(gx)
((f。)。)gxy = f(gxy)
(((f。)。)。)gxyz = f(gxyz)
(((f。)。)。)。)。)gxyzv = f(gxyzv)
...

下面是为((nub。)。)的这个标识的派生:

 (f。g)x = f(gx)

(nub。)::等式a1 => (a - > [a1]) - > a - > [a1]
(nub。)= \ g x - > (nub(g x))

((nub。)。)::方程a2 => (a - > a1 - > [a2]) - > a - > a1 - > [a2]
((nub。)。)=((\ gx - >(nub(gx)))。)=(\ g'x' - >(\ gx - > (nub(gx)))(g'x'))
= \'g'x'x - > (nub((g'x')x))

有一个关于这个(和相关的)成语,但它是用俄语: - (


I want to do something fairly simple;I am using the operator (++) with Data.Map insertWith, and it works fine, but I want to eliminate duplicates in the value created, so want to compose it with nub.

I tried (nub (++)), (nub $ (++)), (nub . (++)), all to no avail, in that the type of (++) does not match the expected type of nub ( [a] ).

I could of course define an auxiliary function or a lambda, but I think that probably there is a composition which would be clearer.

Hints please!

解决方案

You can write this as

((nub .) .) (++)

Example:

Prelude Data.List> ((nub .) .) (++) [1,2,3] [3,4,5]
[1,2,3,4,5]

In general, you have

(f . ) g x = f (g x)
((f . ) . ) g x y = f (g x y)
(((f . ) . ) . ) g x y z = f (g x y z)
((((f . ) . ) . ) . ) g x y z v = f (g x y z v)
...

Here's the derivation of this identity for ((nub .) .):

(f . g) x = f (g x)

(nub .) :: Eq a1 => (a -> [a1]) -> a -> [a1]
(nub .) = \g x -> (nub (g x))

((nub .) .) :: Eq a2 => (a -> a1 -> [a2]) -> a -> a1 -> [a2]
((nub .) .) = ((\g x -> (nub (g x))) .) = (\g' x' -> (\g x -> (nub (g x))) (g' x'))
            = \g' x' x -> (nub ((g' x') x))

There is a nice article about this (and related) idioms, but it's in Russian :-(

这篇关于与二元运算符组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-06 18:18