-1

我正在用 Haskell 写一个 Brainf*ck 解释器。

我正在尝试打印(chr (fromEnum $ getMem state))这只是 Word8 到 Char 的转换。然后我想在特定情况下打印后返回一个新状态,即

    '.' -> do hPutChar stdout (chr (fromEnum $ getMem state))
            hFlush stdout
            return state { prog_pointer = prog_pointer state}

我收到此错误消息

The function `hPutChar' is applied to six arguments,
but its type `Handle -> Char -> IO ()' has only two

In a stmt of a 'do' block:
  hPutChar
    stdout
    (chr (fromEnum $ getMem state))
    hFlush
    stdout
    return
    (state {prog_pointer = prog_pointer state})

使用此代码

iterateBF :: BFState -> IO BFState
iterateBF state = case (program state !! prog_pointer state) of
    --some more cases here--
    '.' -> do hPutChar stdout (chr (fromEnum $ getMem state))
            hFlush stdout
            return state { prog_pointer = prog_pointer state}

我似乎无法弄清楚为什么我会收到此错误。

在 leftaroundabout 和 sabauma 发表评论后,我将代码编辑为

iterateBF :: BFState -> IO BFState
iterateBF state = case (program state !! prog_pointer state) of
    --some more cases here--
    '.' -> do hPutChar stdout (chr (fromEnum $ getMem state))
           hFlush stdout
           return state { prog_pointer = prog_pointer state}

这次只使用空格。

但是,我得到parse error on input 'hFlush'

有谁知道为什么?

4

1 回答 1

2

基于leftaroundabout的评论,你想要

'.' -> do hPutChar stdout (chr (fromEnum $ getMem state))
          hFlush stdout
          return state { prog_pointer = prog_pointer state}

注意 后面的所有行是如何do相互对齐的,而不是与do.

于 2012-12-01T21:36:06.750 回答