2

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"
meltuhamy
  • 3,293
  • 4
  • 23
  • 22

3 Answers3

7

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).

Jon Purdy
  • 53,300
  • 8
  • 96
  • 166
3

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"
hammar
  • 138,522
  • 17
  • 304
  • 385
-1

The following code works:

import System.Process
import System.Environment

main = do
   args <- getArgs
   let inputfname = head args
   system $ "rm -f "++ inputfname ++ " functions.txt"

The reasons were explained by other commenters.

nponeccop
  • 13,527
  • 1
  • 44
  • 106