home *** CD-ROM | disk | FTP | other *** search
/ PSION CD 2 / PsionCDVol2.iso / Programs / 876 / hugs.sis / Maybe.hs < prev    next >
Encoding:
Text File  |  2000-09-21  |  1.3 KB  |  47 lines

  1. -----------------------------------------------------------------------------
  2. -- Standard Library: Operations on the Maybe datatype
  3. --
  4. -- Suitable for use with Hugs 98
  5. -----------------------------------------------------------------------------
  6. module Maybe(
  7.     isJust, isNothing,
  8.     fromJust, fromMaybe, listToMaybe, maybeToList,
  9.     catMaybes, mapMaybe,
  10.  
  11.     -- ... and what the Prelude exports
  12.     Maybe(Nothing, Just),
  13.     maybe
  14.     ) where
  15.  
  16. isJust              :: Maybe a -> Bool
  17. isJust (Just a)        = True
  18. isJust Nothing         = False
  19.  
  20. isNothing             :: Maybe a -> Bool
  21. isNothing Nothing      = True
  22. isNothing (Just a)     = False
  23.  
  24. fromJust              :: Maybe a -> a
  25. fromJust (Just a)      = a
  26. fromJust Nothing       = error "Maybe.fromJust: Nothing"
  27.  
  28. fromMaybe             :: a -> Maybe a -> a
  29. fromMaybe d Nothing    = d
  30. fromMaybe d (Just a)   = a
  31.  
  32. maybeToList           :: Maybe a -> [a]
  33. maybeToList Nothing    = []
  34. maybeToList (Just a)   = [a]
  35.  
  36. listToMaybe           :: [a] -> Maybe a
  37. listToMaybe []         = Nothing
  38. listToMaybe (a:as)     = Just a
  39.  
  40. catMaybes             :: [Maybe a] -> [a]
  41. catMaybes ms           = [ m | Just m <- ms ]
  42.  
  43. mapMaybe              :: (a -> Maybe b) -> [a] -> [b]
  44. mapMaybe f             = catMaybes . map f
  45.  
  46. -----------------------------------------------------------------------------
  47.