gpt4 book ai didi

haskell - 条件中的意外分号

转载 作者:行者123 更新时间:2023-12-03 14:54:36 24 4
gpt4 key购买 nike

我有以下代码行,当使用 GHC 编译时,它会顺利进行:

addRDF c (Just (FILE))  = do
(_:file:_) <- getArgs
check <- doesFileExist file
if check then do rdfG <- TLI.readFile file >>= (return . parseN3fromText)
case rdfG of (Left s) -> putStrLn s
(Right g) -> storeRDF c g
else do putStrLn "Specified files does not exist"

但是当我通过 cabal 构建过程运行它时,它会输出以下错误。
Repository/Adder.hs:46:35:
Unexpected semi-colons in conditional:
if check then do { rdfG <- TLI.readFile file
>>=
(return . parseN3fromText);
case rdfG of {
(Left s) -> putStrLn s
(Right g) -> storeRDF c g } }; else do { putStrLn
"Specified files does not exist" }
Perhaps you meant to use -XDoAndIfThenElse?

我可以在错误中看到额外的分号,但我不明白它来自哪里。

这是我的 cabal 配置文件:
cabal-version: >= 1.2
build-type: Simple

library
build-depends:
base,
containers,
HTTP >= 4000.2.2,
directory >= 1.1.0.0,
text >= 0.11.1.13,
swish >= 0.6.5.2
exposed-modules: Repository.Adder, Repository.Configuration
ghc-options: -Wall

executable repository-add
main-is: repository-add.hs
build-depends:
MissingH,
swish >= 0.6.5.2,
split >= 0.1.4.2
ghc-options: -Wall

更新
if 的缩进正确:
addRDF c (Just (FILE))  = do (_:file:_) <- getArgs
check <- doesFileExist file
if check
then do rdfG <- TLI.readFile file >>= (return . parseN3fromText)
case rdfG of (Left s) -> putStrLn s
(Right g) -> storeRDF c g
else do putStrLn "Specified files does not exist"

我在 check 之后得到一个分号现在也是:
Repository/Adder.hs:46:35:
Unexpected semi-colons in conditional:
if check; then do { rdfG <- TLI.readFile file
>>=
(return . parseN3fromText);
case rdfG of {
(Left s) -> putStrLn s
(Right g) -> storeRDF c g } }; else do { putStrLn
"Specified files does not exist" }
Perhaps you meant to use -XDoAndIfThenElse?

最佳答案

您的缩进不正确,但是当您使用原始 GHC 编译器时它可以工作,因为它会自动打开错误消息中提到的语法扩展 ( DoAndIfThenElse )。

在 Cabal 中,您必须在代码文件的顶部或在 Cabal 文件中手动指定您使用的语言扩展;否则,编译器将不会启用它们。

if 子句缩进的一个正确版本是这样的:

if check
then do
rdfG <- TLI.readFile file >>= (return . parseN3fromText)
case rdfG of
(Left s) -> putStrLn s
(Right g) -> storeRDF c g
else putStrLn "Specified files does not exist"

您必须保留 then部分和 else比它们所在的 block 更深的缩进级别。

关于haskell - 条件中的意外分号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10076318/

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