不在范围内:数据构造函数 [英] Not in scope: data constructor

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

问题描述

我用 haskell 编写了一个程序,但是我得到了ghci的错误。这里是源代码,如果我有

pre $ p $ :: Prop
p1 =和(Var'A')(Not (Var'A'))

它会显示 A&& 〜A ,这是源代码

 导入Data.List 
导入数据。 Char
数据Prop = Const Bool |
Var Char |
不是道具|
和Prop Prop |
或Prop Prop |
隐含支出支出
导出等式
实例显示支出其中
显示(Var Char)=显示Char
显示(非支柱)=(〜++显示Prop ++)
show(and Prop Prop)=(++ show prop ++&&++ show Prop ++)
show(or Prop Prop )=(++ show prop||++ show prop ++)
show(Imply Prop Prop)=(++ show prop=>show Prop ++ )

我从ghci得到了两个主要错误...

 不在范围内:数据构造函数`Char'
不在范围内:数据构造函数`Prop'

我是haskell的初学者,非常感谢。

解决方案

以大写字母开头的值名称保留给构造函数,例如 Var True False 等等。变量必须以小写字母开头。



另外,您不能为两个不同的v使用相同的名称ariables。如果Haskell在每次使用它时都知道你的意思是什么?你不能简单地使用构造函数的定义作为函数中的模式;您需要为每个字段分别指定一个名称。

因此,不是 Var Char ,而是写 Var name ;而不是 Imply Prop Prop ,编写 Imply pq (或 Imply prop1 prop2 )等等。


I wrote a program with haskell but I got errors from ghci

here is the source codes,I construct it, and if I have

p1 :: Prop
p1 = And (Var 'A') (Not (Var 'A'))

It will show A && ~A so that is the source codes

import Data.List
import Data.Char
data Prop = Const Bool | 
        Var Char | 
        Not Prop | 
        And Prop Prop | 
        Or Prop Prop | 
        Imply Prop Prop
        deriving Eq
instance Show Prop where
  show (Var Char) = show Char
  show (Not Prop) = "(~" ++ show Prop ++ ")"
  show (And Prop Prop) = "(" ++ show Prop ++ "&&" ++ show Prop ++ ")"
  show (Or Prop Prop) = "(" ++ show Prop "||" ++ show Prop ++ ")"
  show (Imply Prop Prop) = "(" ++ show Prop "=>" show Prop ++ ")"

And I got two main errors from ghci...

Not in scope: data constructor `Char'
Not in scope: data constructor `Prop'

I am a beginner with haskell,thankyou very much.

解决方案

Value names that start with an uppercase letter are reserved for constructors, like Var, True, False, etc. Variables must start with a lowercase letter.

Additionally, you can't use the same name for two different variables. How would Haskell know which one you meant each time you used them? You can't simply use the definition of a constructor as a pattern in a function; you need to give a separate name to each field.

So, instead of Var Char, write Var name; instead of Imply Prop Prop, write Imply p q (or Imply prop1 prop2), and so on.

这篇关于不在范围内:数据构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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