views:

275

answers:

1

Hi all,

Okay, so I am making a program in Haskell that needs to change certain words based on two command line arguments. I have made the replace function and everything works great, but I am stumped getting it to work with command line arguments.

Here is the main code: (replace function not included)

main = do 

text <- getContents

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

So for intstance in the terminal I want to be able to type something like: "---> cat textfile.txt | ./replace oldword newword"

I know this code is close since I have seen others do it this way. O_o

Thanks for any help

+5  A: 

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)
sth
Sorry about the ambiguous question. =P It is my first time posting ever for help with source code, Haskell is so alien to me. That seems to have worked. I knew wit would something minor like that.Thank you so much for the help, I can finish off this program now. =D
Survot