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.
  2. Add the following data type for representing the days in a week:
        data Days = Sunday | Monday | Tuesday | Wednesday | Thursday |         Friday | Saturday deriving Show
  1. Now, add the Variant type that takes five type arguments:
        data Variant a b c d e = Variant0                        | Variant1 a                       | Variant2 b                       | Variant3 c                       | Variant4 d                       | Variant5 e                       deriving Show
  1. Now, use the preceding types in our main function:
        main :: IO ()        main = do        putStrLn $ "Sum Type 1 : Showing days of the week"        putStrLn $ show [Sunday, Monday, Tuesday, Wednesday, Thursday,        Friday, Saturday]        putStrLn $ "Days type can have only 7 values"        putStrLn ""        putStrLn "Sum Type 2 : Variant with 5 possible data          constructors" putStrLn "Each constructor contribues ...

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