为什么会出现解析错误,如何在haskell中解决此错误?

问题描述 投票:0回答:1

我是Haskell的新手,在阅读真实世界的Haskell时遇到了一个问题:

Q)使用前面第71页的“简单命令行框架”一节中的命令框架,编写一个程序来打印其输入的每一行的第一个单词。

命令框架是:

-- file: ch04/InteractWith.hs
-- Save this in a source file, e.g., Interact.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"
-- replace "id" with the name of our function below
myFunction = id

我对这个问题的解决方法是:

-- file: ch04/InteractWith.hs
-- Save this in a source file, e.g., Interact.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"
-- replace "id" with the name of our function below
myFunction = concat (map (++"\n") (map head (map words (lines input))))

按照说明,每当我尝试使用ghc --make filename编译该程序时,都会出现解析错误,即

error: parse error on input ‘args’
   |
36 |       args <- getArgs
   |       ^^^^
haskell io haskell-stack ghci args
1个回答
0
投票

您的缩进已关闭。

© www.soinside.com 2019 - 2024. All rights reserved.