Haskellのmplusってもしかしてすごい便利じゃない?

束縛(<-)とcaseでインデントが多くネストしてしまいそうな式でも、少ないインデントで済ませられる。

Maybeで使う

import Control.Monad (liftM2)
import Control.Monad (MonadPlus(..))

main :: IO ()
main = do
  print $ Nothing  `mplus` return 1
  print $ return 1 `mplus` (return 2 :: Maybe Int)
  print $ Nothing  `mplus` (Nothing  :: Maybe Int)
  let x = return (return 1) :: IO (Maybe Int)
  let y = return Nothing    :: IO (Maybe Int)
  print =<< liftM2 mplus x y
Just 1
Just 1
Nothing
Just 1

(IO Maybe)で使う

fooDBFile :: (MonadIO m, MonadThrow m) => m (Maybe FilePath)
fooDBFile = do
  let maybeDir = liftM2 mplus (getEnv "XDG_DATA_DIR") (getEnv "HOME")
  case maybeDir of
    dir     -> dir ++ "/foo.db"
    Nothing -> error

ベンリ。

筆者プロフィール

my-latest-logo

aiya000(あいや)

せつラボ 〜圏論の基本〜」 「せつラボ2~雲と天使と関手圏~」 「矢澤にこ先輩といっしょに代数!」を書いています!

強い静的型付けとテストを用いて、バグを防ぐのが好き。Haskell・TypeScript。