Haskell:读取多个命令行参数

-1 haskell command-line-arguments

好的,所以我在Haskell中创建一个程序,需要根据两个命令行参数更改某些单词.我已经做了替换功能,一切都很好,但我很难用它来使用命令行参数.

这是主要代码:(替换功能不包括在内)

main = do 

text <- getContents

(command1:command2:_) <- getArgs
putStrLn (replace (read command1) (read command2) text)
Run Code Online (Sandbox Code Playgroud)

所以对于终端中的intstance,我希望能够键入类似:"---> cat textfile.txt | ./replace oldword newword"

我知道这段代码很接近,因为我看到其他人这样做了.O_O

谢谢你的帮助

sth*_*sth 10

您应该在问题中包含您正在获得的错误类型或不符合预期的错误.只是说"我很难过"并没有给出太多暗示会出现什么问题.

所以一个疯狂的猜测:可能你的replace函数将字符串作为参数.由于getArgs已经将参数作为字符串返回,因此无需调用read,这会将这些字符串转换为另一种数据类型.只需直接使用参数:

main = do 
    text <- getContents

    (command1:command2:_) <- getArgs
    putStrLn (replace command1 command2 text)
Run Code Online (Sandbox Code Playgroud)