Does the main function in Haskell always start with main = do?
In java, we always write:
public static void main(String[] args){...}
when we want to start writing a program.
My question: is it the same for Haskell, IE: can I always declare: main = do when I want to write code for a program in Haskell?
eg:
main = do
putStrLn "What your name?"
name <- getLine
putStrLn ("Hello " ++ name)
This program will ask the user, "What's your name?" the user input will then be stored inside the variable name and the "Hello" ++ name will be displayed until the program ends.
source to share
Short answer : No , we have to announce main =
, but not do
.
main
should be an IO type (sic IO a
) where a
is arbitrary (since it is ignored), as written here :
It is important to use a name
main
: itmain
is defined as the entry point of a Haskell program (similar to a functionmain
in C) and must be ofIO
type , usuallyIO ()
.
But you don't need the notation do
. In fact, it is syntactic sugar . Yours is actually: do
main
main =
putStrLn "What your name?" >> getLine >>= \n -> putStrLn ("Hello " ++ n)
Or more elegantly:
main = putStrLn "What your name?" >> getLine >>= putStrLn . ("Hello " ++)
So, here we have written a main
unmarked do
. For more information on desurage do
see here .
source to share
Yes, if you do
have multiple lines in your block , and if you even use the notation do
.
The full do
-notation syntax also includes curly braces and semicolons:
main = do { putStrLn "What your name?"
; name <- getLine
; putStrLn ("Hello " ++ name)
}
With them, padding doesn't matter. So when you only have one line of IO code like
main = do { print "Hello!" }
there are no semicolons, no indentation to pay attention to, and curly braces and keyword do
become redundant.
do
converts to IO
-monadic code, but you can view it as implementation details first. In fact, you should.
"Monad" is a scary and uninformative word, but it really is nothing more than EDSL, conceptually. Embedded domain-specific language means that we have Haskell's own values โโworth for (in this case) I / O computation. We write our I / O programs in this language, which become native Haskell values, which we can operate on like any other Haskell eigenvalue - collect them in lists, compose them into more complex descriptions (programs) of calculations, etc. .d.
The value main
is one of those values โโcalculated by our Haskell program. The compiler sees this and executes the I / O program it designates at runtime.
The fact is that now we can have a "function" getCurrentTime
(impossible, at first glance, in the functional paradigm, since it must return different results for individual calls), since it does not return the current time - the action described by him will do this when the described the I / O program is started by the runtime system.
At the type level, this is reflected by values โโthat are not just one simple Haskell type a
, but a parameterized type IO a
, tagged IO
as belonging to this particular world of I / O programming.
source to share