简体   繁体   中英

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” head . words “one two three four” throws an exception and function composition 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 . The other way to compile it is to put head . words head . words in parentheses but (head . words) :: String -> String has the same type as head . words :: String -> String head . words :: String -> String so why putting it in parentheses makes the expression compile?

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”) ie words applied on a string must produce a function (as demanded by (.) ). But that's not the type that words has:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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