gpt4 book ai didi

haskell - 了解 `getArgs`

转载 作者:行者123 更新时间:2023-12-04 09:24:35 25 4
gpt4 key购买 nike

我期待以下 GetArgs.hs打印出传递给它的参数。

import System.Environment

main = do
args <- getArgs
print args

但是,在 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

由于 print有这种类型:

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

我会期待 print args工作过。

为什么不?

最佳答案

print args工作正常。什么不起作用是main 3 4 3 . main不接受任何参数,但你试图用三个来调用它。

如果你想通过 ghci 的命令行参数模拟调用你的程序,你可以使用 :main命令(前面有一个冒号)。或者,您可以编译您的程序并使用给定的参数从命令行实际运行它。

关于haskell - 了解 `getArgs`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24543263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com