Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parse error on input ‘->’ in 'case of' statement in Haskell

module SpaceAge (Planet(..), ageOn) where

data Planet = Mercury
            | Venus
            | Earth
            | Mars
            | Jupiter
            | Saturn
            | Uranus
            | Neptune

ageOn :: Planet -> Float -> Float
ageOn planet seconds = 
  case planet of {
    Mercury -> seconds * 0.2408467
    Venus -> seconds * 0.61519726
    Earth -> seconds * 1
    Mars -> seconds * 1.8808158
    Jupiter -> seconds * 11.862615
    Saturn -> seconds * 29.447498
    Uranus -> seconds * 84.016846
    Neptune -> seconds * 164.79132
  }

Hello, when I compile the above Haskell Code, it gives me

error: parse error on input ‘->’
    |
115 |     Venus -> seconds * 0.61519726

All my cases are vertically aligned, so what’s the issue?

Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Writing curly braces indicates to GHC that you don’t want to use whitespace layout for determining the block structure, so then you would have to use semicolons to separate each of the cases.

But the preferred way is to just leave out the curly braces:

ageOn' :: Planet -> Float -> Float
ageOn' planet seconds = 
  case planet of
    Mercury -> seconds * 0.2408467
    Venus -> seconds * 0.61519726
    Earth -> seconds * 1
    Mars -> seconds * 1.8808158
    Jupiter -> seconds * 11.862615
    Saturn -> seconds * 29.447498
    Uranus -> seconds * 84.016846
    Neptune -> seconds * 164.79132
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading