計算機科学のブログ

コンテキストでの型の操作 Applicative型クラス:関数をコンテキスト内で使用する コンテキストでの部分適用に<*>を使用する <*>演算子

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT5(コンテキストでの型の操作)、LESSON 28(Applicative型クラス:関数をコンテキスト内で使用する)、28.2(コンテキストでの部分適用に<>を使用する)、<>演算子、クイックチェック 28-3の解答を求めてみる。

コード

lesson/app/Main.hs

module Main where

import Lib ()

nums :: [Maybe Int]
nums = Nothing : map Just [2 .. 5]

numsPair :: [(Maybe Int, Maybe Int)]
numsPair = (,) <$> nums <*> nums

main :: IO ()
main = do
  mapM_
    ( \(x, y) ->
        putStrLn $
          mconcat
            [ "(*) <$> ",
              show x,
              " <*> ",
              show y,
              " = ",
              show $ (*) <$> x <*> y
            ]
    )
    numsPair
  mapM_
    ( \(x, y) ->
        putStrLn $
          mconcat
            [ "div <$> ",
              show x,
              " <*> ",
              show y,
              " = ",
              show $ div <$> x <*> y
            ]
    )
    numsPair
  mapM_
    ( \(x, y) ->
        putStrLn $
          mconcat
            [ "mod <$> ",
              show x,
              " <*> ",
              show y,
              " = ",
              show $ mod <$> x <*> y
            ]
    )
    numsPair

入出力結果(Terminal, Zsh)

% stack exec lesson-exe
(*) <$> Nothing <*> Nothing = Nothing
(*) <$> Nothing <*> Just 2 = Nothing
(*) <$> Nothing <*> Just 3 = Nothing
(*) <$> Nothing <*> Just 4 = Nothing
(*) <$> Nothing <*> Just 5 = Nothing
(*) <$> Just 2 <*> Nothing = Nothing
(*) <$> Just 2 <*> Just 2 = Just 4
(*) <$> Just 2 <*> Just 3 = Just 6
(*) <$> Just 2 <*> Just 4 = Just 8
(*) <$> Just 2 <*> Just 5 = Just 10
(*) <$> Just 3 <*> Nothing = Nothing
(*) <$> Just 3 <*> Just 2 = Just 6
(*) <$> Just 3 <*> Just 3 = Just 9
(*) <$> Just 3 <*> Just 4 = Just 12
(*) <$> Just 3 <*> Just 5 = Just 15
(*) <$> Just 4 <*> Nothing = Nothing
(*) <$> Just 4 <*> Just 2 = Just 8
(*) <$> Just 4 <*> Just 3 = Just 12
(*) <$> Just 4 <*> Just 4 = Just 16
(*) <$> Just 4 <*> Just 5 = Just 20
(*) <$> Just 5 <*> Nothing = Nothing
(*) <$> Just 5 <*> Just 2 = Just 10
(*) <$> Just 5 <*> Just 3 = Just 15
(*) <$> Just 5 <*> Just 4 = Just 20
(*) <$> Just 5 <*> Just 5 = Just 25
div <$> Nothing <*> Nothing = Nothing
div <$> Nothing <*> Just 2 = Nothing
div <$> Nothing <*> Just 3 = Nothing
div <$> Nothing <*> Just 4 = Nothing
div <$> Nothing <*> Just 5 = Nothing
div <$> Just 2 <*> Nothing = Nothing
div <$> Just 2 <*> Just 2 = Just 1
div <$> Just 2 <*> Just 3 = Just 0
div <$> Just 2 <*> Just 4 = Just 0
div <$> Just 2 <*> Just 5 = Just 0
div <$> Just 3 <*> Nothing = Nothing
div <$> Just 3 <*> Just 2 = Just 1
div <$> Just 3 <*> Just 3 = Just 1
div <$> Just 3 <*> Just 4 = Just 0
div <$> Just 3 <*> Just 5 = Just 0
div <$> Just 4 <*> Nothing = Nothing
div <$> Just 4 <*> Just 2 = Just 2
div <$> Just 4 <*> Just 3 = Just 1
div <$> Just 4 <*> Just 4 = Just 1
div <$> Just 4 <*> Just 5 = Just 0
div <$> Just 5 <*> Nothing = Nothing
div <$> Just 5 <*> Just 2 = Just 2
div <$> Just 5 <*> Just 3 = Just 1
div <$> Just 5 <*> Just 4 = Just 1
div <$> Just 5 <*> Just 5 = Just 1
mod <$> Nothing <*> Nothing = Nothing
mod <$> Nothing <*> Just 2 = Nothing
mod <$> Nothing <*> Just 3 = Nothing
mod <$> Nothing <*> Just 4 = Nothing
mod <$> Nothing <*> Just 5 = Nothing
mod <$> Just 2 <*> Nothing = Nothing
mod <$> Just 2 <*> Just 2 = Just 0
mod <$> Just 2 <*> Just 3 = Just 2
mod <$> Just 2 <*> Just 4 = Just 2
mod <$> Just 2 <*> Just 5 = Just 2
mod <$> Just 3 <*> Nothing = Nothing
mod <$> Just 3 <*> Just 2 = Just 1
mod <$> Just 3 <*> Just 3 = Just 0
mod <$> Just 3 <*> Just 4 = Just 3
mod <$> Just 3 <*> Just 5 = Just 3
mod <$> Just 4 <*> Nothing = Nothing
mod <$> Just 4 <*> Just 2 = Just 0
mod <$> Just 4 <*> Just 3 = Just 1
mod <$> Just 4 <*> Just 4 = Just 0
mod <$> Just 4 <*> Just 5 = Just 4
mod <$> Just 5 <*> Nothing = Nothing
mod <$> Just 5 <*> Just 2 = Just 1
mod <$> Just 5 <*> Just 3 = Just 2
mod <$> Just 5 <*> Just 4 = Just 1
mod <$> Just 5 <*> Just 5 = Just 0
%