confused about composite function in haskell -


let f = show.sum.map read.words f "1 2" 

it work.

show.sum.map read.words "1 2" 

i errors:

<interactive>:19:19:     couldn't match expected type ‘a -> [string]’                 actual type ‘[string]’     relevant bindings include       :: -> string (bound @ <interactive>:19:1)     possible cause: ‘words’ applied many arguments     in second argument of ‘(.)’, namely ‘words "1 2"’     in second argument of ‘(.)’, namely ‘map read . words "1 2"’ prelude> :t show.sum.map show.sum.map   :: (num [b], show b, foldable ((->) [a])) => (a -> b) -> string prelude> show.sum.map read.words "1 2"      <interactive>:21:19:         couldn't match expected type ‘a -> [string]’                     actual type ‘[string]’         relevant bindings include           :: -> string (bound @ <interactive>:21:1)         possible cause: ‘words’ applied many arguments         in second argument of ‘(.)’, namely ‘words "1 2"’         in second argument of ‘(.)’, namely ‘map read . words "1 2"’     prelude>  

i want know why? (show.sum.map read.words) "1 2" can work.

the issue application binds more tightly application in fact has highest precedence of haskell operator. means code parses

 show.sum.map read.(words "1 2") 

which doesn't type check because words "1 2" has type string, not can composed .. in order fix can use $ operator designed behave application lowest precedence instead of highest.

 show.sum.map read.words $ "1 2" 

will work intended because has correct association. in general why you'll see pattern in haskell code (well @ least haskell code) of foo . bar . baz $ quux quite frequently.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -