Parse errors in do-blocks
When I try to load the following code to ghci
import System.Environment (getArgs)
import System.IO
interactWith function inputFile outputFile = do
input <- readFile inputFile
writeFile outputFile (function input)
main = mainWith myFunction
where mainWith function = do
args <- getArgs
case args of
[input,output] -> interactWith function input output
_ -> putStrLn "error: exactly two arguments needed"
myFunction = id
it produces error: parse error on input ‘args’
. But it is not clear to me what is producing the problem, and I am unable to divine what the problem is by looking at the answers to questions on parse errors.
Why is an error produced?
Solution 1:
You need to fix the indentation of your program. The items under the do
block should be indented at least one column more to the right than the start of the definition in the where
clause. You should also indent the cases under the case … of
clause:
import System.Environment (getArgs)
import System.IO
interactWith function inputFile outputFile = do
input <- readFile inputFile
writeFile outputFile (function input)
main = mainWith myFunction
where mainWith function = do
args <- getArgs
case args of
[input,output] -> interactWith function input output
_ -> putStrLn "error: exactly two arguments needed"
myFunction = id