The composition of the function and $ - one compiles, the other does not.

Why is this compiling 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 is not so:

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

This is exactly the same for me. Errors in the second case:

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/"'
+4
source share
2 answers

(.)more strongly bound than (=<<), but ($)weaker bound than (=<<). Therefore, the first expression can be written as

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

and the second is like

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

So, in the last expression, a function =<<that expects something like its first argument a -> m bis given L.take 500, which is a function from ByteStringto ByteString. This is not consistent with each other and this is an error message.

+17
source

(.) ($) , :

(.) :: (b->c) -> (a->b) -> (a->c)
($) :: (b->c) ->   b    ->   c

b ~ (a->b) c ~ (a->c), " ", " ", .

Prelude> let g a b = let x = a . b ; y = a $ b in undefined

<interactive>:1:24:
    Occurs check: cannot construct the infinite type: a = a1 -> a
    ....

, , , - :

> : (.)
(.):: (b → c) → (a → b) → a → c - GHC.Base
infixr 9.
Prelude > : ($)
($):: (a → b) → a → b - GHC.Base
infixr 0 $

: , . $ . , f $ g $ ... $ h $ x f . g . ... . h $ x.

,

 f $ g $ x = f $ (g $ x) = ($) f (($) g x) 

 f . g $ x = (f . g) $ x = ($) ((.) f g) x

. ($) (.); , .

+3

Source: https://habr.com/ru/post/1548481/


All Articles