我的解释器支持以下操作:
up
(增加y
坐标),down
(减少y
坐标),left
(减少x
坐标),right
(增加x
坐标),printX
(打印x
坐标),printY
(打印y
坐标)。我目前的实现看起来像这样
interpreterHelper :: [String] -> Int -> Int -> Int -> [String] -> [String]
interpreterHelper cmds index x y output
| index == length (cmds) = reverse output
| (cmds !! index) == "up" = interpreterHelper cmds (index + 1) x (y + 1) output
| (cmds !! index) == "down" = interpreterHelper cmds (index + 1) x (y - 1) output
| (cmds !! index) == "left" = interpreterHelper cmds (index + 1) (x - 1) y output
| (cmds !! index) == "right" = interpreterHelper cmds (index + 1) (x + 1) y output
| (cmds !! index) == "printX" = interpreterHelper cmds (index + 1) x y (show x : output)
| (cmds !! index) == "printY" = interpreterHelper cmds (index + 1) x y (show y : output)
interpreter :: [String] -> [String]
interpreter cmds = interpreterHelper cmds 0 0 0 []
但是,我的练习要求我可以通过以下语句使其交互:
interact (unlines . interpreter . lines)
这似乎没有发生。
我怎样才能使它具有交互性,这样当我输入
printY
时,我会立即收到 y
- 坐标的当前值的输出?