0
data Months = January|February|March|April|May|June|July
         deriving (Eq, Ord, Enum)
instance Show Months where 

show January="Jan"
show February="Feb"
show March="Mar"
....

What do I need to do to show the output as such: ”Jan”, ”Feb”, ..., ”July" ?

2
  • 2
    it is unclear what you are asking - do you need help with your Show instance - it is wrongly indented use at least 2 spaces before each show ...; if you mean how to print it on command line - use print January or putStrLn $ show January Commented Mar 20, 2016 at 17:37
  • 1
    I would also call your datatype Month - as the plural would usually indicate a list of things (this is just convention). Commented Mar 20, 2016 at 17:38

1 Answer 1

1

Show can be derived automatically:

data Months = January | February | March | April | May | June | July
              deriving (Eq, Ord, Enum, Show)

And so:

λ> show January 
"January"

I suppose there might be a good reason that you want your Show instance to display those abbreviated month names, but why not just change the names of your data constructors to match?

Sign up to request clarification or add additional context in comments.

3 Comments

instead of show January, can I have it so that when I write show Months, they are all displayed? @mdunsmuir
@benjy1911 Use show [January .. July]. The Enum instance allows you to generate all of the months in sequence.
Or, better yet, derive also Bounded and show [minBound .. maxBound] , just in case more months are added later.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.