简体   繁体   English

F# 中的 Haskell `$` 可能吗?

[英]Haskell `$` in F# possible?

In Haskell, we can write在 Haskell 中,我们可以写

print $ abs $ 3 - 5

using $ .使用$

In F#, we can write在 F# 中,我们可以写

printfn "%d" << abs <| 3 - 5

However, in many cases in F#, it would be also useful to have the same functionality of $ since the above are simply expressions with binary operators.但是,在 F# 中的许多情况下,具有与$相同的功能也很有用,因为上述只是带有二元运算符的表达式。

The trick of Haskell $ is that has somewhat the lowest precedence among its binary operators. Haskell $的技巧是它的二进制运算符中的优先级最低。

So, I investigated again于是,我再次调查

https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/symbol-and-operator-reference/#operator-precedence https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/symbol-and-operator-reference/#operator-precedence

and I observe, unfortunately, that there are no operators in lower precedences that can overload safely because all of them seem essential.不幸的是,我观察到没有低优先级的运算符可以安全地过载,因为它们似乎都是必不可少的。

Do you have any ideas on this?你对此有什么想法吗?

Do you think Haskell $ in F# is possible??您认为 F# 中的 Haskell $是可能的吗?

Like Haskell, F♯ also allows you to define new infix operators, not just overload existing ones.与 Haskell 一样,F♯ 还允许您定义新的中缀运算符,而不仅仅是重载现有的运算符。 Unlike Haskell however, you can't select precedence (aka fixity) at will, nor select any non-letter Unicode symbol. Unlike Haskell however, you can't select precedence (aka fixity) at will, nor select any non-letter Unicode symbol. Instead, you must choose from !相反,您必须从! , $ , % , & , * , + , - , . , $ , % , & , * , + , - , . , / , < , = , > , ? , / , < , = , > , ? , @ , ^ , | , @ , ^ , | . . The fixity is then determined by analogy with the standard operators, in a way I don't entirely understand but what does seem to hold is that for any single-symbol infix, you can make a custom one with the same fixity by adding a .然后通过与标准运算符的类比来确定固定性,以一种我不完全理解的方式,但似乎确实存在的是,对于任何单符号中缀,您可以通过添加一个具有相同固定性的自定义中缀. in front.在前。

So, to get a lowest-fixity operator you'd have to call it .|因此,要获得最低固定性运算符,您必须调用它.| . . However, |然而, | is left-associative, so you couldn't write printfn "%d".| abs.| 3 - 5是左结合的,所以你不能写printfn "%d".| abs.| 3 - 5 printfn "%d".| abs.| 3 - 5 printfn "%d".| abs.| 3 - 5 . printfn "%d".| abs.| 3 - 5 However I'd note that in Haskell, your example would also rather be written print. abs $ 3 - 5但是我会注意到,在 Haskell 中,您的示例也宁愿写成print. abs $ 3 - 5 print. abs $ 3 - 5 , and that can indeed be expressed in F♯: print. abs $ 3 - 5 ,这确实可以用 F♯ 表示:

let (.|) f x = f x
printfn "%d" << abs .| 3 - 5

To transliterate print $ abs $ 3 - 5 , you'd need a right -associative operator.要音译print $ abs $ 3 - 5 ,您需要一个关联运算符。 The lowest-precedence custom right-associative operator I can manage to define is .^ , which indeed gets the example working:我可以设法定义的最低优先级自定义右关联运算符是.^ ,它确实使示例工作:

let (.^) f x = f x
printfn "%d" .^ abs .^ 3 - 5

However, this operator doesn't have very low precedence, in particular it actually has higher precedence than the composition operators!但是,此运算符的优先级并不低,尤其是它实际上比组合运算符具有更高的优先级!


Really, you shouldn't be doing any of this and instead just use printfn "%d" << abs <| 3 - 5真的,你不应该这样做,而只是使用printfn "%d" << abs <| 3 - 5 printfn "%d" << abs <| 3 - 5 as you originally suggested. printfn "%d" << abs <| 3 - 5如您最初建议的那样。 It's standard, and it also corresponds to the preferred style in Haskell.它是标准的,也对应于 Haskell 中的首选样式。

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

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