Saturday 4 June 2016

Haskell: Extract the value from Maybe data type

Following program takes Maybe Integer and return the int value.

mayBeEx.hs
getMayBeValue :: Maybe Integer -> Integer
getMayBeValue x =
    case x of
          Nothing -> 0
          Just val -> val

*Main> :load mayBeEx.hs
[1 of 1] Compiling Main             ( mayBeEx.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> let x = Just 102
*Main> let y = Nothing
*Main> 
*Main> getMayBeValue x
102
*Main> 
*Main> getMayBeValue y
0
*Main>


Previous                                                 Next                                                 Home

No comments:

Post a Comment