I'm trying to concatenate a string given as an argument (using getArgs) to the haskell program, e.g.:
"rm " ++ filename ++ " filename2.txt" which is inside a main = do block.
The problem is with the type of filename, and ghc won't compile it, giving an error.
I get an error Couldn't match expected type [a] against inferred type IO ExitCode
the code we're trying to run is:
args <- getArgs
let inputfname = head args
system "rm -f "++ inputfname ++ " functions.txt"
You need $:
system $ "rm -f "++ inputfname ++ " functions.txt"
Or parentheses:
system ("rm -f " ++ inputfname ++ " functions.txt")
Otherwise you’re trying to run this:
(system "rm -f ") ++ inputfname ++ " functions.txt"
It fails because ++ wants [a] (in this case String) but gets IO ExitCode (from system).
The problem is that function application has higher precedence than the (++) operator, so it parses as
(system "rm -f ") ++ inputfname ++ " functions.txt"
while what you meant was
system ("rm -f " ++ inputfname ++ " functions.txt")
or simply
system $ "rm -f " ++ inputfname ++ " functions.txt"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With