理解`getArgs`

Kev*_*ith 7 haskell

我希望以下内容GetArgs.hs打印出传递给它的参数.

import System.Environment

main = do
    args <- getArgs
    print args
Run Code Online (Sandbox Code Playgroud)

但是,加载后ghci,我收到以下错误:

ghci> main 3 4 3

<interactive>:39:1:
    Couldn't match expected type `a0 -> a1 -> a2 -> t0'
                with actual type `IO ()'
    The function `main' is applied to three arguments,
    but its type `IO ()' has none
    In the expression: main 3 4 3
    In an equation for `it': it = main 3 4 3
Run Code Online (Sandbox Code Playgroud)

既然print有这种类型:

ghci>:t print print ::显示=> a - > IO()

我本以期待print args工作.

为什么不?

sep*_*p2k 11

print args工作良好.什么不起作用main 3 4 3.main没有任何争论,但你试图用三个来称呼它.

如果要使用ghci中的命令行参数模拟调用程序,可以使用该:main命令(前面带冒号).或者,您可以编译您的程序,并使用给定的参数从命令行实际运行它.


小智 11

在ghci中使用run命令

:run main 3 4 3