File: Maybe.hs

package info (click to toggle)
hugs 1.4.199801-1
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 7,220 kB
  • ctags: 5,609
  • sloc: ansic: 32,083; haskell: 12,143; yacc: 949; perl: 823; sh: 602; makefile: 236
file content (42 lines) | stat: -rw-r--r-- 1,432 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
-----------------------------------------------------------------------------
-- Standard Library: Operations on the Maybe datatype
--
-- Suitable for use with Hugs 1.4.
-----------------------------------------------------------------------------
module Maybe(
    isJust, fromJust, fromMaybe, listToMaybe, maybeToList,
    catMaybes, mapMaybe, unfoldr ) where

isJust		      :: Maybe a -> Bool
isJust (Just a)        = True
isJust Nothing         = False

fromJust              :: Maybe a -> a
fromJust (Just a)      = a
fromJust Nothing       = error "Maybe.fromJust: Nothing"

fromMaybe             :: a -> Maybe a -> a
fromMaybe d Nothing    = d
fromMaybe d (Just a)   = a

maybeToList           :: Maybe a -> [a]
maybeToList Nothing    = []
maybeToList (Just a)   = [a]

listToMaybe           :: [a] -> Maybe a
listToMaybe []         = Nothing
listToMaybe (a:as)     = Just a
 
catMaybes             :: [Maybe a] -> [a]
catMaybes ms           = [ m | Just m <- ms ]

mapMaybe              :: (a -> Maybe b) -> [a] -> [b]
mapMaybe f             = catMaybes . map f

unfoldr               :: ([a] -> Maybe ([a], a)) -> [a] -> ([a],[a])
unfoldr f x            = case f x of
                           Nothing     -> (x,[])
                           Just (x',y) -> let (ys,x'') = unfoldr f x'
                                          in  (x'',y:ys)

-----------------------------------------------------------------------------