Haskell - 同时打印并返回状态

时间:2012-12-01 21:05:49

标签: haskell compiler-errors syntax-error

我正在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}

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

在左下角和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'

任何人都知道为什么?

1 个答案:

答案 0 :(得分:2)

建立在leftaroundabout的评论上,你想要

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

请注意do之后的所有行是如何相互对齐的,而不是do