Search code examples
haskellfunctional-programmingparse-error

Why am I getting parse error on myFunction


In the following code, I'm getting parse error on input `myFunction':

import System.Environment (getArgs)
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"
        -- replace "id" with the name of our function below       
        myFunction = id 

Solution

  • myFunction = id needs to line up with the other thing defined in the where clause.

    main = mainWith myFunction  
     where mainWith function = do         
         ^  args <- getArgs          
         |  case args of            
         |      [input,output] -> interactWith function input output         
         |      _ -> putStrLn "error: exactly two arguments needed"
         | -- replace "id" with the name of our function below       
         | myFunction = id 
         | ^
         | |
         | here
         |
         not here
    

    The same code with more indentation to make it clear:

    import System.Environment (getArgs)
    interactWith function inputFile outputFile = do  
        input <- readFile inputFile 
        -- I assume a line break belongs here 
        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"
              -- replace "id" with the name of our function below       
              myFunction = id