0

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?

John J. Camilleri
  • 4,171
  • 5
  • 31
  • 41
mohammad
  • 90
  • 2
  • 12

1 Answers1

3

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

Christian Conkle
  • 5,932
  • 1
  • 28
  • 52
  • I ommited intent, now error changed to: parse error on input '=' in line for definition of `translate` function. – mohammad Nov 16 '14 at 18:42
  • I used `&` just for cast the result of translate function to pure string, or IO string, likely to solve this problem. This function just recieve PGF file that made from 2 GF files. – mohammad Nov 16 '14 at 18:57
  • That's not how Haskell works. The output of `translate` is a `String`, which is exactly what `writeFile` wants. – Christian Conkle Nov 16 '14 at 18:59
  • I used brackets instead of `&`, but still the error(`parse error on input '=' ` in line for definition) exists. Do you think the logic of this code is correct? – mohammad Nov 16 '14 at 19:12
  • You need to indent the body of `main` by at least one space. Otherwise `translate` is parsed as part of the `do` block in `main`, which is syntactically invalid. – Christian Conkle Nov 16 '14 at 21:21
  • No problem; the layout rule can be confusing, especially if you're coming from a C-style language. Check out the [Haskell wikibook chapter on indentation](http://en.wikibooks.org/wiki/Haskell/Indentation). – Christian Conkle Nov 16 '14 at 21:31