I expect the following code to convert "15" into a integer and print the result, but it throws an error.
main = print $ read "15" :: Integer
Couldn't match expected type `Integer' with actual type `IO ()'
But just using main = print (read "15" :: Integer)
runs fine. I was under the impression that $ effectively surrounds the rest of the line in brackets. Why doesn't $ work in this case?
$
is not a syntax sugar that puts (
in current place and )
in the end of the line.
So print $ read "15" :: Integer
is interpreted like (print (read "15")) :: Integer
. It happens because $ :: (a -> b) -> a -> b
(functional composition infix operator) takes two functions print
and read "15"
and «apply» them one by another. :: Integer
seems to be not a function here, it is more like a keyword, so $
doesn't work the way you expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With