Haskell编译器错误:不在范围内

usr*_*usr 3 haskell

我试图通过编写一个简单的文件副本util来学习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"
Run Code Online (Sandbox Code Playgroud)

这让我感到高兴

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>
Run Code Online (Sandbox Code Playgroud)

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

这是一个repro文件:在电子邮件

sth*_*sth 13

看起来您混合了标签和空格(只需在"编辑"视图中查看您的问题即可查看问题).当您的编辑器均匀地缩进代码时,编译器似乎对选项卡的宽度有不同的解释,导致该writeFile destPath contents行另外缩进.所以源代码解释如下:

  ...
  putStrLn ("Copying from " ++ srcPath ++ " to " ++ destPath ++ "...")
  contents <- readFile srcPath writeFile destPath contents
  putStrLn "Finished"
Run Code Online (Sandbox Code Playgroud)

在此源代码的解释contents是在创建之前使用,因此您会收到编译器错误.

为了避免这些错误,最好不要使用制表符,或者至少需要额外注意一致使用它们.