Function composition and $ - one compiles, another doesn't

haskell

Solution

`(.)` binds stronger than `(=<<)` and `($)` binds weaker than `(=<<)`. Therefore the first expression can be written as

main = (L.putStrLn . L.take 500) =<< simpleHttp "http://stackoverflow.com/"

and the second as

main = L.putStrLn $ (L.take 500 =<< simpleHttp "http://stackoverflow.com/")

So in the latter expression, the function `=<<`, which expects something like `a -> m b` as its first argument, is given `L.take 500`, which is a function from `ByteString` to `ByteString`. This doesn't fit together and thats what the error message says.

Problem

Why does this compile well: ``` import Network.HTTP.Conduit (simpleHttp) import qualified Data.ByteString.Lazy.Char8 as L main = L.putStrLn . L.take 500 =<< simpleHttp "http://stackoverflow.com/" ``` but this doesn't: ``` main = L.putStrLn $ L.take 500 =<< simpleHttp "http://stackoverflow.com/" ``` For me these are exactly the same. The errors in the 2nd case are: ``` Couldn't match type `L.ByteString' with `m0 b0' Expected type: L.ByteString -> m0 b0 Actual type: L.ByteString -> L.ByteString In the return type of a call of `L.take' In the first argument of `(=<<)', namely `L.take 500' In the second argument of `($)', namely `L.take 500 =<< simpleHttp "http://stackoverflow.com/"' Couldn't match expected type `L.ByteString' with actual type `m0 b0' In the second argument of `($)', namely `L.take 500 =<< simpleHttp "http://stackoverflow.com/"' ```

Original source

Related problems