Expr
算術演算のクラスを作ります
class Expr a where
mul :: a -> a -> a
add :: a -> a -> a
lit :: Integer -> a
このようなものを「解析」したい: mul ( add (lit 3) (lit 2)) (lit 4) = (3+2)*4
そして私はデータ型を持っています:
data StackExp = PushI Integer
| PushB Bool
| Add
| Mul
| And
| Or
deriving Show
と
type Program = [StackExp] --i use this type for function of stack calculator later
私の仕事は次のとおりです。Expr
for タイプのインスタンスを作成する必要がありますProgram
より具体的に - この変換を行いたい:
mul ( add (lit 3) (lit 2)) (lit 4)
->>>[PushI 2, PushI 3, Add, PushI 4, Mul]
[[StackExp]]
インスタンス宣言の出力で受け取るため、問題があります。
私の試み:
instance Expr Program where
lit n = (PushI n):[]
add exp1 exp2 = exp1:(exp2:[Add])
mul exp1 exp2 = exp1:(exp2:[Mul])
すべての部分式をリストに連結する方法がわかりません
---------------- コンパイラ エラーは次のようになります------------------------
Couldn't match type `[StackExp]' with `StackExp'
Expected type: StackExp
Actual type: Program
..........