简体   繁体   English

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

[英]Application of arguments to function composition in Haskell

Being a newbie to Haskell I can't understand why the expression head . words “one two three four” 作为Haskell的新手我无法理解为什么表达head . words “one two three four” head . words “one two three four” throws an exception and function composition head . words head . words “one two three four”抛出异常和功能组成head . words head . words must be applied with $ operator - the expression on the right of it doesn't need further evaluation because it's just a single String . 必须使用$运算符应用head . words - 右侧的表达式不需要进一步评估,因为它只是一个String The other way to compile it is to put head . words 编译它的另一种方法是放head . words head . words in parentheses but (head . words) :: String -> String has the same type as head . words :: String -> String 括号中的head . words但是(head . words) :: String -> Stringhead . words :: String -> String类型相同head . words :: String -> String head . words :: String -> String so why putting it in parentheses makes the expression compile? head . words :: String -> String所以为什么把它放在括号中会使表达式编译?

Because of precedence rules. 因为优先规则。 Application has highest precedence; 应用程序具有最高优先级; $ - lowest. $ - 最低。

head . words “one two three four” head . words “one two three four” is parsed as head . (words “one two three four”) head . words “one two three four”被解析为head . (words “one two three four”) head . (words “one two three four”) ie words applied on a string must produce a function (as demanded by (.) ). head . (words “one two three four”)即应用于字符串的words必须产生一个函数(如(.)所要求的)。 But that's not the type that words has: 但这不是words的类型:

Prelude> :t words
words :: String -> [String]

head . words $ “one two three four” head . words $ “one two three four” on the other hand, is parsed as (head . words) “one two three four” and the types fit. 另一方面, 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM