IO 定义你的程序主要操作
要使 Haskell 程序可执行,你必须提供具有 IO () 类型的 main 函数的文件
main::IO ()
main = putStrLn "Hello world!"
编译 Haskell 时,它会检查 IO 数据并将其转换为可执行文件。当我们运行这个程序时,它将打印 Hello world!。
如果你的 IO a 类型的值不是 main,他们将不会做任何事情。
other::IO ()
other = putStrLn "I won't get printed"
main::IO ()
main = putStrLn "Hello world!"
编译该程序并运行它将与上一个示例具有相同的效果。other 中的代码被忽略。
为了使 other 中的代码具有运行时效果,你必须将其组合成 main。没有最终组成 main 的 IO 值将具有任何运行时效果。要按顺序组合两个 IO 值,你可以使用 do-notation:
other::IO ()
other = putStrLn "I will get printed... but only at the point where I'm composed into main"
main::IO ()
main = do 
  putStrLn "Hello world!"
  other
编译并运行此程序时,它会输出
Hello world!
I will get printed... but only at the point where I'm composed into main
请注意,操作的顺序由 other 如何组成 main 而不是定义顺序来描述。