带有中点符号的自由点样式 [英] Point free style with infix notation
问题描述
您好,有没有一种在使用中缀表示法时编写无点样式的方法?
Hello is there a way to write point free style when using infix notation?
f::Int->Int->Int->Int
f a b=(+) (a+b)
为什么您不能做这样的事情?
Why you cannot do something like this ?
f::Int->Int->Int->Int
f a b=(a+b) +
or
f a b= (a+b) `+`
您是否不能将诸如点之类的运算符组合在一起?
Can you not combine operators in point free style like e.g?
ptfree::Int->Int->Int->Int
ptfree=(+) (+)
我的意思是您可以砍碎像fold
这样的函数的参数,但是为什么不算运算符参数呢?
I mean you can chop arguments of functions like fold
but why not for operator arguments?
推荐答案
好了,因为您需要传递两个参数,所以我们可以使用所谓的"惊讶的猫头鹰运算符 em>".这基本上是参数的组合.所以我们可以使用:
Well since you need to pass two parameters, we can use what is known as the "surprised owl operator". This is basically a composition of parameters. So we can use:
f = ((.).(.)) (+) (+)
或者我们可以像这样进一步内联运算符:
Or we can more inline the operator like:
f = ((+) .) . (+)
猫头鹰运算符((.).(.)) f g
基本上是\x y -> f (g x y)
The owl operator ((.).(.)) f g
basically is short for \x y -> f (g x y)
这是如何工作的?
"惊讶的猫头鹰算子"的规范形式为:
= ((.) . (.))
------------- (canonical form)
(.) (.) (.)
因此,我们现在可以将(.)
替换为相应的lambda表达式:
So we can now replace the (.)
s with corresponding lambda expressions:
(\f g x -> f (g x)) (.) (.)
所以现在我们可以执行一些替换:
So now we can perform some replacements:
(\f g x -> f (g x)) (.) (.)
-> (\x -> (.) ((.) x))
-> (\x -> (\q r y -> q (r y)) ((.) x))
-> (\x -> (\r y -> ((.) x) (r y)))
-> (\x r y -> ((.) x) (r y))
-> (\x r y -> ((\s t u -> s (t u)) x) (r y))
-> (\x r y -> (\t u -> x (t u)) (r y))
-> (\x r y -> (\u -> x ((r y) u)))
-> \x r y u -> x ((r y) u))
-> \x r y u -> x (r y u)
所以从根本上讲,这意味着我们惊讶的猫头鹰算子等于:
So basically it means that our surprised owl operator, is equal to:
surprised_owl :: (y -> z) -> (a -> b -> y) -> a -> b -> z
surprised_owl f g x y = f (g x y) -- renamed variables
如果现在我们通过提供的功能(两次(+)
)对它进行专门化处理,我们将得到:
And if we now specialize this with the fuctions provided (two times (+)
), we get:
f = surprised_owl (+) (+)
如此:
f x y = (+) ((+) x y)
这篇关于带有中点符号的自由点样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!