計算機科学のブログ

Haskell - コンテキストでの型の操作 - コンテキストとしてのリスト:Applicative型クラスをさらに掘り下げる - Applicative型クラス

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)の UNIT5(コンテキストでの型の操作)、LESSON 29(コンテキストとしてのリスト:Applicative型クラスをさらに掘り下げる)、29.1(Applicative型クラス)、クイックチェック 29-1の解答を求めてみる。

コード

sample1.hs

main :: IO ()
main = do
  mapM_ (\(x, y) -> putStrLn $ mconcat [show x, " ++ ", show y, " = ",
                                        show ((++) <$> x <*> y)])
        (mconcat $ map (\x -> map (\y -> (x, y))
                             [Nothing, Just "b"])
                       [Nothing, Just "a"])

入出力結果(Terminal, Zsh)

% runghc sample1.hs 
Nothing ++ Nothing = Nothing
Nothing ++ Just "b" = Nothing
Just "a" ++ Nothing = Nothing
Just "a" ++ Just "b" = Just "ab"
%