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. We will experiment in the main function in this file. Replace the main function with following content:
        main :: IO ()        main = do        putStrLn "Using Maybe"
  1. Continue in the same function. Start defining various values of Maybe. Maybe is a sum type that may contain a value. The data constructor Just takes the value, whereas the constructor Nothing represents the absence of any value. Define three instances of the Maybe value, representing integral values 10, 2, and 0:
        let i = Just 10 :: Maybe Int        j = Just 2 :: Maybe Int        z = Just 0 :: Maybe Int
  1. Note the indentation. Since it is part of the same function main, the indentation should match the putStrLn "Using Maybe" line. 
  1. Use the isJust function to check ...

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