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
Post a Comment