haskell - Real World Haskell 示例的编译错误

标签 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

最佳答案

错误的缩进。 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

关于haskell - Real World Haskell 示例的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18603094/

相关文章:

haskell - 从任意未知 Nats 中提取值

haskell - 堆配置文件中的微量元素是什么?

C 或 Haskell 库在 bmp 文件中查找表单

haskell - 自由对象是如何构造的?

Haskell 为替代的 Either 数据类型定义 Functor 实例

haskell - 在 Haskell 中使用类型类访问相似数据类型的字段

具有多个参数和类型问题的 Haskell 映射

haskell - Haskell中的固定长度循环缓冲区

haskell - 关于应用程序的多个参数不起作用?

haskell - 类型系统中的强制Maybes