Search code examples
haskellparse-error

paper, rock, and scissors in Haskell


The codes below is programmed to decide the result of paper rock and scissors in Haskell, however terminal give an error

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

here is the error message from terminal

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

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

Who likes to tell me what is wrong with it? Thanks XD


Solution

  • Your code is well formed I can load it into ghci without any modification.
    The parse error rely on how your text editor manage space and tab.
    You should investigate more on your editor configuration than on the correctness of your base code.


    Use this chunk of code to reformat you file.

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

    and write,

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

    Then recompile the code.
    Let me know if it solve the issue.

    As said it should not be enough, but if you enter this instruction into ghci.

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

    And you show us the raw result, maybe it could help.