デバッグ機能に問題があります。以下にある特定のコマンドの実行を定義することになっています。次に、実行が呼び出されるたびに現在のメモリ値を再帰的に出力するデバッグ関数を作成する必要があります。
exec(execution)、debug、およびデータ型のコードは次のとおりです
data Com = Ass Char Exp
| While Exp Com
| Seq Com Com
deriving Show
exec :: Memory -> Com -> Memory
exec m (Ass c e) = update m (c, (eval m e))
exec m (While e c) = if (eval m e) == 0 then m
else exec (exec m c) (While e c)
exec m (Seq c1 c2) = exec (exec m c1) c2
debug :: Memory -> Com -> [Memory]
debug m (Ass c e) = update m (c, (eval m e)) : [m]
debug m (Seq c1 c2) = (exec (exec m c1) c2) :[m]
debug m (While e c) = if (eval m e) == 0 then [m]
else (exec (exec m c) (While e c)) : [m]
com1 :: Com
com1 = Seq (Ass 'z' (Num 1))
(While (Var 'y') (Seq (Ass 'z' (Mul (Var 'z') (Var 'y')))
(Ass 'y' (Add (Var 'y') (Num (-1))))))
関数を特定のメモリ状態とコマンドで実行すると、開始メモリと終了メモリのみが出力されます。たとえば、debug [('y',4)] com1
すべてを実行した場合は[[('y',0),('z',24)],[('y',4)]]
、印刷する必要があります。
[('y',4)]
[('y',4),('z',1)]
[('y',4),('z',4)]
[('y',3),('z',4)]
[('y',3),('z',12)]
[('y',2),('z',12)]
[('y',2),('z',24)]
[('y',1),('z',24)]
[('y',1),('z',24)]
[('y',0),('z',24)]
再帰的に出力するには、デバッグ関数で何を変更する必要があるのかを尋ねたいのですが。