Haskell的纸,岩石和剪刀

lib*_*bra 1 haskell parse-error

下面的代码被编程来决定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
Run Code Online (Sandbox Code Playgroud)

这是来自终端的错误消息

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

test.hs:1:60: parse error on input `data'
Failed, modules loaded: none.
Run Code Online (Sandbox Code Playgroud)

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

zur*_*rgl 5

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


使用此代码块重新格式化您的文件.

$ cd /to/your/code.hs
$ ghci
Run Code Online (Sandbox Code Playgroud)

和写,

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

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

如上所述它应该不够,但是如果你把这个指令输入ghci.

ghci > readFile "code.hs"
-- print something
Run Code Online (Sandbox Code Playgroud)

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