跳转至内容

Haskell/Solutions/Alternative 和 MonadPlus

摘自 Wikibooks,开放世界的开放图书

← 返回至 Alternative 和 MonadPlus

1.

-- law 1: mzero `mplus` m  =  m
-- law 2: m `mplus` mzero  =  m
-- law 3: m `mplus` (n `mplus` o)  =  (m `mplus` n) `mplus` o

Nothing `mplus` (Just x) = Just x
(Just x) `mplus` Nothing = Just x
(Just x) `mplus` ((Just y) `mplus` (Just z)) = ((Just x) `mplus` (Just y)) `mplus` (Just z)

[] `mplus` [x] = [x]
[x] `mplus` [] = [x]
[x] `mplus` ([y] `mplus` [z]) = ([x] `mplus` [y]) `mplus` [z]

2.

hexChar :: String -> Maybe (Char, String)
hexChar s = digitParse s `mplus` alphaParse s
    where digitParse s = do let (c':s') = s
                            x <- msum $ map ($ s) (map digit [0..9])
                            return (intToDigit x, s')
          alphaParse s = msum $ map ($ s) (map char (['a'..'f'] ++ ['A'..'F']))

使用 Alternative 的另一种方式来执行此操作

-- You can also replace asum with msum and <|> with mplus
hexChar :: String -> Maybe (Char, String)
hexChar s =
    let x = asum $ map ($ s) (map char "0123456789")
        y = asum $ map ($ s) (map char "abcdefABCDEF")
    in x <|> y

3.

safeLog :: (Ord b, Floating b) => b -> Maybe b
safeLog x = guard (x > 0) *> pure (log x)
华夏公益教科书