Parse error on input,while compiling read File, process it and write result to another File code in Haskell

1.4k views Asked by At

When I compile below code in Haskell, I get following error:

Error: parse error on input 'gr' in the line x

 module Main where

 import PGF
 import System.Environment
 import System.IO

 main :: IO ()
 main = do
 file:_ <- getArgs
 gr     <- readPGF file
 content <- readFile "input.txt"
 writeFile "output.txt" &(translate gr content)


 translate :: PGF -> String -> String
    translate gr s = case parseAllLang gr (startCat gr) s of
    (lg,t:_):_ -> unlines [linearize gr l t | l <- languages gr, l /= lg]
    _ -> "NO PARSE"

In this code I want to read a line(string) from input file and bind it to content. after that pass the content and PGF file(gr) to translate function and finally write the processed string via translate function on output file.

What is wrong with this code, and how can I fix it?

1

There are 1 answers

6
Christian Conkle On BEST ANSWER

Don't indent the definition of translate. It should line up directly below its type signature.

translate :: PGF -> String -> String
translate gr s = {- ... -}

Do indent the body of main. The line after do needs to be indented, otherwise the layout rule will dictate that everything following it is part of the do block.

I think you're borrowing & from another language. You should write that line as either

writeFile "output.txt" (translate gr content)

or

writeFile "output.txt" $ translate gr content

(Which are identical; the operator $ is used in Haskell to eliminate the need for parentheses.)