When calculating IO (IO ())
, both (IO ())
and ()
is calculated, so why
main :: IO (IO ())
main = print <$> (print "Hello, World!")
"Hello, World!"
not
IO "Hello, World!" -- ??
"Hello, World!"
printf() is a library function to send formatted output to the screen. In this program, printf() displays Hello, World! text on the screen. The return 0; statement is the "Exit status" of the program.
In many programming languages, print is a function that sends text, variables, or another object to the screen. If you're more familiar with the programming language Scratch, print is equivalent to say.
Strings in python are surrounded by either single quotation marks, or double quotation marks. 'hello' is the same as "hello".
In Python 3, programming a “Hello World” message is as easy as typing print('Hello World') into the Python console: >>> print('Hello World') Hello World. Note that single and double quotes may be used interchangeably in Python.
main :: IO (IO ())
main = print <$> (print "Hello, World!")
is equivalent, thanks to the monad laws, to
main :: IO (IO ())
main = do
result <- print "Hello, World!"
return (print result)
Now, print
always returns ()
as result, so the whole code is equivalent to
main :: IO (IO ())
main = do
_ <- print "Hello, World!"
return (print ())
Finally, the result of main
is simply discarded. That is, the last line could be return (putStrLn "this is ignored")
and have the same effect.
Hence the code will only execute the first print "Hello, World!"
.
I would recommend that you always define main :: IO ()
. Haskell allows us to declare main :: IO AnyTypeHere
, but this is (IMO) confusing.
I would also recommend you use putStrLn
, and not print
to print strings, since the latter will quote and escape the whole string.
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