在Real World Haskell示例中编译错误

时间:2013-09-03 23:32:07

标签: haskell

我正在写一段来自“Real World Haskell”的代码:

 ghc --make ch04/InteractWith.hs 
[1 of 1] Compiling Main             ( ch04/InteractWith.hs, ch04/InteractWith.o )

ch04/InteractWith.hs:9:5: parse error on input `args'

dan @dbmint~ / testHaskell $ cat ch04 / InteractWith.hs

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"

myFunction = id

2 个答案:

答案 0 :(得分:7)

错误的缩进。 do-block(args <- getArgs ...部分)与mainWith定义的开头处于同一级别。

编译没有错误:

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"

myFunction = id

答案 1 :(得分:2)

空格已关闭

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"

myFunction = id