haskell - Haskell ReadFile fails for existing files
问题描述
I'm writing a function in Haskell as part of a compiler to open a file, read a set of file names from it and concatenate them into a string. The code runs fine in ghci but fails when compiled with the following:
fact.fn: openFile: does not exist (No such file or directory)
I am completely sure fact.fn exists and it's in the same directory as the executable.
Relevant code:
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))
解决方案
我认为问题在于,在处理如下导入命令时:
import fact.fn
您没有从文件名的开头剥离空间,因此您的程序实际上是在尝试导入文件" fact.fn"
而不是"fact.fn"
. 如果您仔细研究错误消息,您可以验证是否属于这种情况。如果错误是:
*** Exception: fact.fn: openFile: does not exist (No such file or directory)
Exception:
在and之间有两个空格而不是一个fact.fn
,那么文件名的开头会有一个额外的空格。
我完全不知道你是如何在解释器中成功运行它的。
推荐阅读
- git - Git: Is it valid (and presumably forward compatible) to use non-email strings in the user.email config?
- jq - “减少输入”子句中的可选元素
- c++ - 未定义符号:Boost::DLL 教程中的_ZTI13my_plugin_api
- python - 如何使用正则表达式替换一些括号而不是其他括号
- c# - C# 内存处理 - NetworkInterface
- mysql - 如何让 Keycloak 连接到 MySQL DB?
- javascript - 尝试在插入之前检查 sql 表值是否已经存在
- hadoop - 纱线与 Sqoop 在 Java 版本方面“竞争”
- reactjs - Apollo GraphQL React 刷新令牌(使用 AWS 放大)来自 Promise --> flatMap() 根本没有被触发
- c++ - 我在尝试让 debugPrintfEXT 在着色器中工作时遇到问题