gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:36:06 27 4
gpt4 key购买 nike

我正在写一段来自“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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com