[英]Application of arguments to function composition in Haskell
作为Haskell的新手我无法理解为什么表达head . words “one two three four”
head . words “one two three four”
抛出异常和功能组成head . words
必须使用$
运算符应用head . words
- 右侧的表达式不需要进一步评估,因为它只是一个String
。 编译它的另一种方法是放head . words
括号中的head . words
但是(head . words) :: String -> String
与head . words :: String -> String
类型相同head . words :: String -> String
head . words :: String -> String
所以为什么把它放在括号中会使表达式编译?
因为优先规则。 应用程序具有最高优先级; $
- 最低。
head . words “one two three four”
head . words “one two three four”
被解析为head . (words “one two three four”)
head . (words “one two three four”)
即应用于字符串的words
必须产生一个函数(如(.)
所要求的)。 但这不是words
的类型:
Prelude> :t words
words :: String -> [String]
head . words $ “one two three four”
另一方面, head . words $ “one two three four”
被解析为(head . words) “one two three four”
head。words (head . words) “one two three four”
并且类型适合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.