大文字・小文字の反転(その2)

[id:mzp:20090804#maybe]

Maybeで繋げるなら,こんな感じかな.

import Control.Monad
import Data.Char
import Data.Maybe

affect :: (MonadPlus m, Eq a) => (a -> a) -> a -> m a
f `affect` x = if fx == x then mzero else return fx where fx = f x

invertChar :: Char -> Char
invertChar c =  maybe c id $ msum $ map (`affect` c) [toLower,toUpper]

invertString :: String -> String
invertString = map invertChar