O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open src/Main.hs for editing. We will use this file for using type classes. 
  2. Define a data type Month to describe a month in a year:
        data Month = January | February | March | April | May | June           | July | August | September | October | November |               December deriving Show

Note that we have still used automatic derivation from Show. We will illustrate Show later in the recipe.

  1. Next, implement the Enum class. The Enum class is responsible for generating a list of consecutive integers and expressions such as [1..10].  The Enum class provides this behavior by associating with Integer. Create an instance of the Enum class for the data type Month. Essentially, we need to implement two functions, toEnum and fromEnum, to convert from ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required