Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: Brackets work but $ throws an error

Tags:

haskell

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?

like image 392
mcjohnalds45 Avatar asked Jul 28 '13 09:07

mcjohnalds45


1 Answers

$ 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.

like image 139
ДМИТРИЙ МАЛИКОВ Avatar answered Nov 19 '22 16:11

ДМИТРИЙ МАЛИКОВ