-1

好的,所以我在 Haskell 中制作了一个程序,需要根据两个命令行参数更改某些单词。我已经做了替换功能,一切都很好,但我很难让它与命令行参数一起工作。

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

main = do 

text <- getContents

(command1:command2:_) <- getArgs
putStrLn (replace (read command1) (read command2) text)

因此,例如在终端中,我希望能够输入类似:“---> cat textfile.txt | ./replace oldword newword”

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

谢谢你的帮助

4

1 回答 1

10

You should really include in your question what kind of error you are getting or what does not work as expected. Just saying "I'm stumped" doesn't give much hints what goes wrong.

So a wild guess: Probably your replace function takes strings as parameters. Since getArgs already returns the arguments as strings there is no need to call read, which would convert these strings to another datatype. Just use the arguments directly:

main = do 
    text <- getContents

    (command1:command2:_) <- getArgs
    putStrLn (replace command1 command2 text)
于 2010-03-19T12:15:47.660 回答