1

下面的代码被编程来决定 Haskell 中纸石头和剪刀的结果,但是终端给出错误

data Move = Paper | Rock | Scissors  
 deriving (Eq, Show)
data Result = Win | Draw | Loose 
  deriving (Eq, Show)


beats :: Move -> Move
beats move = case move of 
  Paper -> Scissors
  Rock  -> Paper
  Scissors -> Rock

score :: Move -> Move -> Result
score this_move opposing_move
  | this_move == beats opposing_move = Win
  | this_move == opposing_move       = Draw
  | otherwise = Loose

这是来自终端的错误消息

[1 of 1] Compiling Main             ( test.hs, interpreted )

test.hs:1:60: parse error on input `data'
Failed, modules loaded: none.

谁喜欢告诉我它有什么问题?谢谢XD

4

1 回答 1

5

您的代码格式正确,我可以将其加载到 ghci 中而无需任何修改。
解析错误取决于您的文本编辑器如何管理空间和选项卡。
你应该更多地调查你的编辑器配置,而不是你的基本代码的正确性。


使用这段代码重新格式化您的文件。

$ cd /to/your/code.hs
$ ghci

和写,

ghci >let f = readFile "code.hs"
ghci >do {reencoded <- fmap (filter (/= '\r')) f; writeFile "ncode.hs" reencoded}

然后重新编译代码。
让我知道它是否解决了问题。

如前所述,这还不够,但是如果您将此指令输入到 ghci 中。

ghci > readFile "code.hs"
-- print something

你向我们展示了原始结果,也许它会有所帮助。

于 2013-04-14T12:53:38.830 回答