Haskell:需要帮助来调试一个简单的命令行解释器

问题描述 投票:0回答:0

我的解释器支持以下操作:

  • 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
- 坐标的当前值的输出?

haskell functional-programming read-eval-print-loop
© www.soinside.com 2019 - 2024. All rights reserved.