gpt4 book ai didi

Haskell 编译器错误 : not in scope

转载 作者:行者123 更新时间:2023-12-01 07:22:42 27 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Why shouldn't I mix tabs and spaces?

(1 个回答)


5年前关闭。




我正在尝试通过编写一个简单的文件复制工具来学习 Haskell:

main = do
putStr "Source: "
srcPath <- getLine
putStr "Destination: "
destPath <- getLine
putStrLn ("Copying from " ++ srcPath ++ " to " ++ destPath ++ "...")
contents <- readFile srcPath
writeFile destPath contents
putStrLn "Finished"

这让我
GHCi, version 6.10.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main ( D:\Test.hs, interpreted )

D:\Test.hs:8:22: Not in scope: `contents'
Failed, modules loaded: none.
Prelude>

我不明白编译器错误,因为变量似乎没问题。怎么了?

这是一个复制文件: at rapidshare

最佳答案

看起来您混合了制表符和空格(只需在“编辑” View 中查看您的问题即可查看问题)。当您的编辑器查看均匀缩进的代码时,编译器似乎对选项卡应该有多宽有不同的解释,导致 writeFile destPath contents行被额外缩进。所以源代码是这样解释的:

  ...
putStrLn ("Copying from " ++ srcPath ++ " to " ++ destPath ++ "...")
contents <- readFile srcPath writeFile destPath contents
putStrLn "Finished"

本源码解读 contents在创建之前使用,因此您会收到编译器错误。

为避免此类错误,最好不要使用选项卡,或者至少要格外小心,始终如一地使用它们。

关于Haskell 编译器错误 : not in scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1694097/

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