涉及预期类型的​​错误

时间:2019-02-17 13:55:04

标签: haskell

以下代码(摘自Graham Hutton的“在Haskell中编程”(第9章))

(?<=<center><b><br>)(.*)(?=<\/b><\/center>)

产生这些错误:

:{
Prelude| data Op = Add | Sub | Mul | Div
Prelude| instance Show Op where
Prelude|    show Add = "+"
Prelude|    show Sub = "-"
Prelude|    show Mul = "*"
Prelude|    show Div = "/"
Prelude| valid :: Op -> Int -> Int -> Bool
Prelude| valid Add _ _ = True
Prelude| valid Sub x y = x > y
Prelude| valid Mul _ _ = True
Prelude| valid Div x y = x `mod` y == 0
Prelude| apply :: Op -> Int -> Int -> Int
Prelude| apply Add x y = x + y
Prelude| apply Sub x y = x - y
Prelude| apply Mul x y = x * y
Prelude| apply Div x y = x `div` y
Prelude| data Expr = Val Int | App Op Expr Expr
Prelude| instance Show Expr where
Prelude|       show (Val n) = n
Prelude|       show (App o l r) = brak l ++ show o ++ brak r 
Prelude|                          where
Prelude|                             brak (Val n ) = show n
Prelude|                             brak e = "(" ++ show e ++ ")"
Prelude| :}

我不明白出了什么问题。有人可以帮忙吗?为了处理第一个错误,是否需要将<interactive>:455:22: error: • Couldn't match type ‘Int’ with ‘[Char]’ Expected type: String Actual type: Int • In the expression: n In an equation for ‘Prelude.show’: Prelude.show (Val n) = n In the instance declaration for ‘Show Expr’ <interactive>:456:26: error: • Couldn't match expected type ‘[Char]’ with actual type ‘Int’ • In the first argument of ‘(++)’, namely ‘brak l’ In the expression: brak l ++ show o ++ brak r In an equation for ‘Prelude.show’: Prelude.show (App o l r) = brak l ++ show o ++ brak r where brak (Val n) = show n brak e = "(" ++ show e ++ ")" <interactive>:456:36: error: • Couldn't match expected type ‘[Char]’ with actual type ‘Int’ • In the first argument of ‘(++)’, namely ‘show o’ In the second argument of ‘(++)’, namely ‘show o ++ brak r’ In the expression: brak l ++ show o ++ brak r <interactive>:456:41: error: • Couldn't match expected type ‘Ghci41.Expr’ with actual type ‘Op’ • In the first argument of ‘show’, namely ‘o’ In the first argument of ‘(++)’, namely ‘show o’ In the second argument of ‘(++)’, namely ‘show o ++ brak r’ <interactive>:456:46: error: • Couldn't match expected type ‘[Char]’ with actual type ‘Int’ • In the second argument of ‘(++)’, namely ‘brak r’ In the second argument of ‘(++)’, namely ‘show o ++ brak r’ In the expression: brak l ++ show o ++ brak r <interactive>:458:50: error: • Couldn't match expected type ‘Ghci41.Expr’ with actual type ‘Int’ • In the first argument of ‘show’, namely ‘n’ In the expression: show n In an equation for ‘brak’: brak (Val n) = show n <interactive>:459:38: error: • Couldn't match expected type ‘Int’ with actual type ‘[Char]’ • In the expression: "(" ++ show e ++ ")" In an equation for ‘brak’: brak e = "(" ++ show e ++ ")" In an equation for ‘Prelude.show’: Prelude.show (App o l r) = brak l ++ show o ++ brak r where brak (Val n) = show n brak e = "(" ++ show e ++ ")" <interactive>:459:45: error: • Couldn't match expected type ‘[Char]’ with actual type ‘Int’ • In the first argument of ‘(++)’, namely ‘show e’ In the second argument of ‘(++)’, namely ‘show e ++ ")"’ In the expression: "(" ++ show e ++ ")" <interactive>:459:50: error: • Couldn't match expected type ‘Ghci41.Expr’ with actual type ‘Expr’ NB: ‘Expr’ is defined at <interactive>:453:1-38 ‘Ghci41.Expr’ is defined at <interactive>:246:1-35 • In the first argument of ‘show’, namely ‘e’ In the first argument of ‘(++)’, namely ‘show e’ In the second argument of ‘(++)’, namely ‘show e ++ ")"’ 的类型声明为String?其他错误报告指出了什么?

如果我看到像这些特定错误报告一样的错误报告,对我来说,采用什么样的避免错误报告的好策略是什么?


编辑:

我已经尝试了以下答案中的代码,但是收到以下错误:

n

1 个答案:

答案 0 :(得分:5)

我在这里发现的唯一错误是nInt,因此您需要使用show将其转换为String

instance Show Expr where
    show (Val n) = show n
    show (App o l r) = brak l ++ show o ++ brak r 
        where brak (Val n) = show n
              brak e = "(" ++ show e ++ ")"

此外,您不会显示Op的类型,但是它也应该是Show实例的类型,因此您应该定义一个instance Show Op

例如:

Prelude> data Op = Add | Sub | Mul | Div
Prelude> :{
Prelude| instance Show Op where
Prelude|     show Add = "+"
Prelude|     show Sub = "-"
Prelude|     show Mul = "*"
Prelude|     show Div = "/"
Prelude| :}
Prelude> data Expr = Val Int | App Op Expr Expr
Prelude> :{
Prelude| instance Show Expr where
Prelude|     show (Val n) = show n
Prelude|     show (App o l r) = brak l ++ show o ++ brak r 
Prelude|         where brak (Val n) = show n
Prelude|               brak e = "(" ++ show e ++ ")"
Prelude| :}

然后我们可以像这样查询它:

Prelude> show (App Mul (Val 3) (App Add (Val 3) (Val 2)))
"3*(3+2)"