使用$运算符将两个括号链接在一起 [英] Chaining two parentheses together using $ operator

查看:199
本文介绍了使用$运算符将两个括号链接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能:

  min(max 10 20)(max 30 40)

我可以将其重写为:

  min(max 10 20)$ max 30 40 

但是还有解决这些问题的方法最后一个括号?



好像这不够好,但我不能让思想去,必须有办法做到这一点。 。

解决方案

能够写出:

  min $ max 10 20 $ max 30 40 

然而,这是行不通的,因为 $ 是正确关联的:

  Prelude> :info $ 
($)::(a - > b) - > a - > b - 定义于'GHC.Base'
infixr 0 $

因此,表达式是消除歧义的:

min((max 10 20)max 30 40)

的确, $ 应该更有意义关联。但是,我们不能简单地将 $ 左结合,因为它可能会破坏现有的代码。然而,你可以定义一个新的操作符:

  infixl 0%

(%)::(a - > b) - > a - > b
f%x = fx

main = print(min%max 10 20%max 30 40)



符号紧挨着美式键盘上的 $ 符号。


I have this function:

min (max 10 20) (max 30 40)

I can rewrite this as:

min (max 10 20) $ max 30 40

But is there also a way to resolve these last parentheses?

Not as if this wasn't good enough, but I just can't let the thought go, that there must be some way to do this...

解决方案

It would be nice to be able to write:

min $ max 10 20 $ max 30 40

However, this wouldn't work because $ is right associative:

Prelude> :info $
($) :: (a -> b) -> a -> b   -- Defined in ‘GHC.Base’
infixr 0 $

Hence, the expression is disambiguated as:

min ((max 10 20) max 30 40)

Indeed, it would make more sense for $ to be left associative. However, we can't simply make $ left associative because it might break existing code. Nevertheless, you could define a new operator:

infixl 0 %

(%) :: (a -> b) -> a -> b
f % x = f x

main = print (min % max 10 20 % max 30 40)

The % symbol is right next to the $ symbol on a US keyboard.

这篇关于使用$运算符将两个括号链接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆