[英]main function won't take any arguments in haskell
Hi I want to define a main function in haskell, similar to the main-function in java嗨,我想在haskell中定义一个main函数,类似于java中的main函数
main :: String -> IO()
main args = do
putStrLn args
But this gives me an error when I try to run the program.但是当我尝试运行程序时,这给了我一个错误。
An Haskell program's entry point has the type of IO ()
, not String -> IO ()
. Haskell 程序的入口点的类型是IO ()
,而不是String -> IO ()
。
To get the command-line arguments, use System.Environment
's getArgs
.要获取命令行参数,请使用System.Environment
的getArgs
。
Using do-notation:使用 do-notation:
import System.Environment (getArgs)
main :: IO ()
main = do
args <- getArgs
print args
(Note that print
is used instead of putStrLn
, since args
have the type of [String]
, and not String
.) (请注意,使用print
而不是putStrLn
,因为args
的类型为[String]
,而不是String
。)
I also found this very useful.我也发现这非常有用。
main :: IO ()
main =
do
input <- getContents
solve input
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.