“功能箭头与右边相关”的用处? [英] Usefulness of "function arrows associate to the right"?
问题描述
阅读 http://www.seas。 upenn.edu/~cis194/spring13/lectures/04-higher-order.html 它声明
特别注意那个函数箭头与右边相关,
是,W - > X - > Y - > Z相当于W - >(X - >(Y - > Z))。我们可以
总是在一个类型的最右边的顶级箭头
的周围添加或删除括号。
函数箭头与右侧相关,但作为函数应用关联到左侧,那么这些信息的用处是什么?我觉得我不了解某些事情,对于我来说,这是一个无意义的点,箭头指向右边。由于函数应用程序总是与左边关联,那么这是我应该关注的唯一关联性?
功能箭头与右侧相关,但此信息的用处?
如果您看到类型签名,例如 f:String - > Int - > Bool
您需要知道函数箭头的关联性,以了解 f
的类型真的是:
$如果箭头关联到左侧,则类型表示
(String - > Int) - > Bool
,也就是 f
接受一个函数作为参数并返回一个布尔值。 字符串 - > (int - > Bool)
,也就是 f
将一个字符串作为参数并返回一个函数。 这是一个很大的区别,如果你想使用 f
,你需要知道它是哪一个。由于函数箭头与右边相关,所以您知道它必须是第二个选项: f
接受一个字符串并返回一个函数。
与右侧功能应用关联的功能箭头与左侧关联
这两个选择很好地结合在一起。例如,我们可以从上面调用 f
作为 fanswer42
,这实际上意味着(f答案)42
。所以我们将字符串answer
传递给 f
,它返回一个函数。然后我们将数字 42
传递给该函数,该函数返回一个布尔值。实际上,我们几乎使用 f
作为带有两个参数的函数。
这是标准方法用Haskell中的两个(或更多)参数编写函数,所以这是一个非常常见的用例。由于函数应用程序和函数箭头的关联性,我们可以在不使用括号的情况下编写此常见用例。
Reading http://www.seas.upenn.edu/~cis194/spring13/lectures/04-higher-order.html it states
In particular, note that function arrows associate to the right, that is, W -> X -> Y -> Z is equivalent to W -> (X -> (Y -> Z)). We can always add or remove parentheses around the rightmost top-level arrow in a type.
Function arrows associate to the right but as function application associates to the left then what is usefulness of this information ? I feel I'm not understanding something as to me it is a meaningless point that function arrows associate to the right. As function application always associates to the left then this the only associativity I should be concerned with ?
Function arrows associate to the right but [...] what is usefulness of this information?
If you see a type signature like, for example, f : String -> Int -> Bool
you need to know the associativity of the function arrow to understand what the type of f
really is:
- if the arrow associates to the left, then the type means
(String -> Int) -> Bool
, that is,f
takes a function as argument and returns a boolean. - if the arrow associates to the right, then the type means
String -> (Int -> Bool)
, that is,f
takes a string as argument and returns a function.
That's a big difference, and if you want to use f
, you need to know which one it is. Since the function arrow associates to the right, you know that it has to be the second option: f
takes a string and returns a function.
Function arrows associate to the right [...] function application associates to the left
These two choices work well together. For example, we can call the f
from above as f "answer" 42
which really means (f "answer") 42
. So we are passing the string "answer"
to f
which returns a function. And then we're passing the number 42
to that function, which returns a boolean. In effect, we're almost using f
as a function with two arguments.
This is the standard way of writing functions with two (or more) arguments in Haskell, so it is a very common use case. Because of the associativity of function application and of the function arrow, we can write this common use case without parentheses.
这篇关于“功能箭头与右边相关”的用处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!