haskell - "function arrows associate to the right"有什么用?

标签 haskell

阅读http://www.seas.upenn.edu/~cis194/spring13/lectures/04-higher-order.html它指出

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 [...] what is usefulness of this information?

如果您看到类似 f : String -> Int -> Bool 的类型签名,您需要知道函数箭头的关联性才能了解 f 的类型 确实是:

  1. 如果箭头关联到左侧,则该类型表示 (String -> Int) -> Bool,即 f 接受一个函数作为参数并返回一个 bool 值。
  2. 如果箭头关联到右侧,则该类型表示 String -> (Int -> Bool),即 f 接受一个字符串作为参数并返回一个函数。

这是一个很大的区别,如果你想使用f,你需要知道它是哪一个。由于函数箭头与右侧关联,因此您知道它必须是第二个选项:f 接受一个字符串并返回一个函数。

Function arrows associate to the right [...] function application associates to the left

这两个选择可以很好地结合起来。例如,我们可以将上面的 f 称为 f "answer"42,这实际上意味着 (f "answer") 42。因此,我们将字符串 "answer" 传递给 f,后者返回一个函数。然后我们将数字 42 传递给该函数,该函数返回一个 bool 值。实际上,我们几乎将 f 用作具有两个参数的函数。

这是在 Haskell 中编写具有两个(或更多)参数的函数的标准方法,因此它是一个非常常见的用例。由于函数应用和函数箭头的关联性,我们可以在不使用括号的情况下编写这个常见用例。

关于haskell - "function arrows associate to the right"有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30440377/

相关文章:

haskell - 将 haskell 点列表转换为可以使用 splot 调用的文件

haskell - 用可变变量表示数据类型

haskell - 从什么意义上说,一个函数的定义“定义”比另一个函数少?

haskell - 使用镜头的 3 种或更多类型之间的同构

haskell - 我可以在 Haskell 中自动生成数据值列表吗?

haskell - 带有仆人的数据库支持的 REST API?

c++ - 什么容器真正模仿了 Haskell 中的 std::vector?

Haskell 读取 args 值

haskell - 如何使用自定义App类型代替IO?

class - Haskell 单体可折叠玫瑰树