函数组合参数在Haskell中的应用

6 haskell pointfree function-composition

作为Haskell的新手,我无法理解为什么表达式 head . words “one two three four”抛出异常并且函数组合head . words必须与$运算符一起应用- 右侧的表达式不需要进一步评估,因为它只是一个单独的String.编译它的另一种方法是放入head . words括号但是(head . words) :: String -> String具有相同的类型,head . words :: String -> String为什么将它放在括号中使表达式编译?

Wil*_*ess 11

因为优先规则.应用程序具有最高优先级; $ - 最低.

head . words “one two three four”被解析为head . (words “one two three four”)words应用于字符串必须产生一个函数(如所要求的(.)).但那不是那种类型words:

Prelude> :t words
words :: String -> [String]
Run Code Online (Sandbox Code Playgroud)

head . words $ “one two three four”另一方面,解析为(head . words) “one two three four”和类型适合.

  • @wojtek啊,是的,`(.)::(b - > c) - >(a - > b) - > a - > c`.别忘了,类型签名中的箭头与右侧相关联.它真的是`(.)::(b - > c) - >(a - > b) - >(a - > c)`.想象一下,你有这两个管道,`g :: b-> c`和`h :: a-> b`.很明显,`b`输出进入`b`输入:`(gh)x = g(hx)`ie`(gh):: a-> c`. (2认同)