Haskell编译器错误:不在范围内 [英] Haskell compiler error: not in scope

查看:125
本文介绍了Haskell编译器错误:不在范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  main = do 
putStr我试图通过编写简单的文件来学习haskell util:源:
srcPath< - getLine
putStr目的地:
destPath< - getLine
putStrLn(从++ srcPath ++复制到++ destPath ++...)
内容< - readFile srcPath
writeFile destPath内容
putStrLn完成

这会得到我

  GHCi,版本6.10.4:http:/ /www.haskell.org/ghc/:?寻求帮助
加载包ghc-prim ...链接...完成。
加载软件包整数...链接...完成。
加载程序包库...链接...完成。
[1的1]编译主(D:\Test.hs,解释)

D:\Test.hs:8:22:不在范围内:`contents'
失败,模块加载:无。
前奏>

我不明白编译器错误,因为这个变量好像没问题。有什么不对?



以下是一个repro文件: at rapidshare

解决方案

看起来你混合了制表符和空格(看看你的问题在编辑视图中查看问题)。当您的编辑器均匀缩进查看代码时,编译器似乎对选项卡的宽度应有不同的解释,导致 writeFile destPath内容行被额外缩进。所以源代码就是这样解释的:

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

在创建源代码 contents 之前,会先使用它来解释编译器错误。 / p>

为避免这类错误,最好不要使用标签,或者至少要格外小心,让它们始终如此。


I am trying to learn haskell by writing a simple file copy util:

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

This gets me

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>

I don't understand that compiler error because the variable seems to be ok. What is wrong?

Here is a repro file: at rapidshare

解决方案

It looks like you mixed tabs and spaces (just look at your question in "edit" view to see the issue). While your editor views the code evenly indented, the Compiler seems to have a different interpretation how wide a tab should be, resulting in the writeFile destPath contents line being additionally indented. So the source is interpreted like this:

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

In this interpretation of the source code contents is used before it is created, so you get a compiler error.

To avoid these kind of errors best don't use tabs, or at least take additional care that you use them consistently.

这篇关于Haskell编译器错误:不在范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆