【发布时间】:2020-04-17 01:31:37
【问题描述】:
我是 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