我用haskell写了一个程序,但是从ghci那里得到了错误。
这是源代码,我构建了它,如果我有
p1 :: Prop
p1 = And (Var 'A') (Not (Var 'A'))它将显示A && ~A,这就是源代码
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 ++ ")"我从ghci得到了两个主要的错误...
Not in scope: data constructor `Char'
Not in scope: data constructor `Prop'我是haskell的初学者,非常感谢。
发布于 2012-05-07 10:56:11
以大写字母开头的值名是为构造函数保留的,如Var、True、False等。变量必须以小写字母开头。
此外,您不能对两个不同的变量使用相同的名称。当你每次使用它们时,Haskell如何知道你指的是哪个?您不能简单地将构造函数的定义用作函数中的模式;您需要为每个字段指定单独的名称。
因此,编写Var name;而不是Imply Prop Prop,编写Imply p q (或Imply prop1 prop2),以此类推。
发布于 2012-05-07 10:56:39
稍微修改一下就能让它正常工作:
instance Show Prop where
show (Var c) = [c]
show (Not p) = "(~" ++ show p ++ ")"
show (And p1 p2) = "(" ++ show p1 ++ " && " ++ show p2 ++ ")"
show (Or p1 p2) = "(" ++ show p1 ++ "||" ++ show p2 ++ ")"
show (Imply p1 p2) = "(" ++ show p1 ++ "=>" ++ show p2 ++ ")"https://stackoverflow.com/questions/10476050
复制相似问题