模棱两可的出现`Just' [英] Ambiguous occurrence `Just'

查看:78
本文介绍了模棱两可的出现`Just'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是绝对的初学者.使用emacs浏览LYAH.

I am an absolute beginner. Going through LYAH using emacs.

我当前的设置:

  • Ubuntu 12.04 LTS(使用经验-初学者)
  • GNU Emacs 23(使用经验-初学者)
    • 可以在haskell专业模式下工作

    此处所述的要点2中很难找到说明(带来haskell库)./a>

    Finding difficult to follow instructions (to bring haskell libraries) at Point 2 described here.

    还需要指导来启用 Scion IDE .

    问题:

    .hs代码

    data Maybe a = Nothing | Just a
    

    运行代码时,出现以下错误:

    While running the code, I get following error:

    请忽略拼写错误,最初发布于:

    Please ignore typo error, Originally posted:

    *Main> just "Haha"  
    

    interactive>:339:1:不在范围内:只是"

    interactive>:339:1: Not in scope: `just'

    这是真正的错误(在Tikhon Jelvis评论后添加):

    This is the real error (added after Tikhon Jelvis' comment):

    *Main> Just "Haha"  
    interactive>:341:1:  
        Ambiguous occurrence `Just'  
        It could refer to either `Main.Just',    
                                 defined at /home/optimight/baby.hs:89:26  
                              or `Prelude.Just',  
                                 imported from `Prelude' at /home/optimight/baby.hs:1:1  
                                 (and originally defined in `Data.Maybe')  
    

    推荐答案

    您的错误只是告诉您两个可能的 Just 版本,GHCi不知道哪个一个选择.

    Your error just tells you that there are two possible versions of Just and GHCi does not know which one to pick.

    每个Haskell程序都会隐式导入一大堆函数和数据类型.这些构成了序幕".这些类型之一是 Maybe .这意味着每个程序都可以访问与您定义的类型完全相同的名称.

    Every Haskell program implicitly imports a whole bunch of functions and data types. These form the "prelude". One of these types is Maybe. This means that every single program already has access to a type just like the one you defined, with exactly the same names.

    您可以通过两种方式克服这一问题.最好的选择是只选择其他名称:

    You can overcome this in two ways. The best option would be to just choose different names:

    data MyMaybe a = MyJust a | MyNothing
    

    (我敢肯定,虽然您可以提出比:P更好的名字)

    (I'm sure you can come up with better names than that though :P.)

    另一种选择是显式导入Prelude,隐藏也许:

    The other option would be to import the Prelude explicitly, hiding Maybe:

    import Prelude hiding (Maybe (..))
    

    程序顶部的这一行只是告诉Haskell导入它通常导入的所有内容 Maybe 及其所有构造函数( Just Nothing ).

    This line at the top of your program just tells Haskell to import everything it normally imports except Maybe and all of it's constructors (Just and Nothing).

    现在,您将遇到另一个问题:GHCi不知道如何将您的 Maybe 类型的值呈现为字符串以显示在提示上.您将收到如下错误:

    Now you will run into one more problem: GHCi does not know how to render a value of your Maybe type into a string to show on the prompt. You will get an error like this:

    <interactive>:1:1:
    No instance for (Show (Maybe [Char]))
      arising from a use of `print'
    Possible fix: add an instance declaration for (Show (Maybe [Char]))
    In a stmt of an interactive GHCi command: print it
    

    您需要做的是告诉编译器 Maybe 值如何显示为字符串.幸运的是,这非常容易.实际上,即使计算机可以做到,这也很容易!如果您将类型定义为:

    What you need to do is tell the compiler how a Maybe value looks as a string. Happily, this is extremely easy. In fact, it's so easy even the computer can do it! If you define your type as:

    data Maybe a = Just a | Nothing deriving (Show)
    

    然后,编译器将为您编写一个 show 函数(基本上是其他语言的 toString ).现在,您的原始语句(只需"Haha" )就可以正常工作.

    then the compiler will write a show function (which is basically toString from other languages) for you. Now your original statement (Just "Haha") should work properly.

    此外:启用Scion完全是一个完全不同的问题.在您了解了更多Haskell并实际从事某种更大的项目之前,我不认为这值得您费心.目前,标准的Haskell模式应该绰绰有余.

    Also: enabling Scion is a completely different question entirely. I don't think it's worth bothering with it until you've learned more Haskell and are actually working on some sort of larger project. For now, the standard Haskell mode should be more than enough.

    这篇关于模棱两可的出现`Just&#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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