我有一个关于调试功能的问题。我应该定义下面某些命令的执行,然后我必须创建一个调试函数,该函数在每次执行被调用后递归地打印出当前的内存值。
这是我的 exec(执行)、调试和数据类型的代码
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)]
我想问的问题是我需要在调试功能中进行哪些更改才能使其递归打印?