Haskell - コンテキストでの型の操作 - Functor型クラス - Functorクラスのコンテキストで関数を使用する
入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)の UNIT5(コンテキストでの型の操作)、LESSON 27(Functor型クラス)、27.2(Functorクラスのコンテキストで関数を使用する) クイックチェック 27-2の解答を求めてみる。
コード
sample2.hs
main :: IO ()
main = do
mapM_ (\x -> print (x, maybeReverse1 x, maybeReverse2 x))
[Nothing, Just "", Just "a", Just "ab", Just "abcde"]
maybeReverse1 :: Maybe String -> Maybe String
maybeReverse1 = fmap reverse
maybeReverse2 :: Maybe String -> Maybe String
maybeReverse2 = (reverse <$>)
入出力結果(Terminal, Zsh)
% runghc sample2.hs
(Nothing,Nothing,Nothing)
(Just "",Just "",Just "")
(Just "a",Just "a",Just "a")
(Just "ab",Just "ba",Just "ba")
(Just "abcde",Just "edcba",Just "edcba")
%