我正在作为编译器的一部分在Haskell中编写一个函数,以打开文件,从文件中读取一组文件名并将它们连接为字符串。该代码在ghci中可以正常运行,但是使用以下代码编译时将失败:
fact.fn:openFile:不存在(没有这样的文件或目录)
我完全确定fact.fn存在,并且与可执行文件位于同一目录中。
相关代码:
compile [fileName] = do
(ilude, contents) <- imports fileName
let lude = "prelude.fn" : ilude
prld <- fmap fullPrelude (mapM readFile lude) --seems to fail here,
--but works fine in the interpreter
let newPrld = unlines [prld, "\nend"]
runEvalWith HappyParser.parseExpr fileName contents newPrld
imports :: String -> IO ([String], String)
imports fileName = do
contents <- readFile fileName
let ls = lines contents
let ifile = filter (isPrefixOf "import") ls {-find import string here-}
let contentList = filter (\w -> w `notElem` ifile) ls
let impts = mapMaybe (stripPrefix "import") ifile
return (impts, (unlines contentList) )
fullPrelude :: [String] -> String
fullPrelude [] = ""
fullPrelude xs = unlines( map (procPrelude) xs)
procPrelude :: String -> String
procPrelude pld = unlines(init(words pld))
答案 0 :(得分:3)
我相信问题在于,在处理以下导入命令时:
import fact.fn
您没有从文件名的开头删除空格,因此您的程序实际上是在尝试导入文件" fact.fn"
而不是"fact.fn"
。如果仔细研究错误消息,可以验证是否是这种情况。如果错误是:
*** Exception: fact.fn: openFile: does not exist (No such file or directory)
在Exception:
和fact.fn
之间使用两个空格而不是一个空格,那么文件名的开头会有一个额外的空格。
我完全不知道您如何在解释器中成功运行它。