計算機科学のブログ

コンテキストでの型の操作 do表記を使ってMonadを扱いやすくする do表記を使って同じコードを異なるコンテキストで再利用する 問題を解決するための準備

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT5(コンテキストでの型の操作)、LESSON 31(do表記を使ってMonadを扱いやすくする)、31.2(do表記を使って同じコードを異なるコンテキストで再利用する)、問題を解決するための準備、クイックチェック 31-2の解答を求めてみる。

コード

lesson/app/Main.hs

module Main (main) where

import Lib
  ( Candidate (..),
    Degree (..),
    Grade (..),
    viable,
  )

candidates :: [Candidate]
candidates =
  Candidate
    <$> [10, 11]
      <*> [B .. A]
      <*> [C .. A]
      <*> [BA .. PhD]

main :: IO ()
main = do
  mapM_
    ( \candidate ->
        print $
          mconcat
            [ show candidate,
              ": ",
              show $ viable candidate
            ]
    )
    candidates

lesson/src/Lib.hs

module Lib
  ( viable,
    Candidate (..),
    Grade (..),
    Degree (..),
  )
where

data Grade = F | D | C | B | A
  deriving (Eq, Ord, Enum, Show, Read)

data Degree = HS | BA | MS | PhD
  deriving (Eq, Ord, Enum, Show, Read)

data Candidate = Candidate
  { candidateId :: Int,
    codeReview :: Grade,
    cultureFirt :: Grade,
    education :: Degree
  }
  deriving (Show)

viable :: Candidate -> Bool
viable candidate =
  all
    (== True)
    [ codeReview candidate > B,
      cultureFirt candidate > C,
      education candidate >= MS
    ]

入出力結果(Terminal, Zsh)

% stack exec lesson-exe
"Candidate {candidateId = 10, codeReview = B, cultureFirt = C, education = BA}: False"
"Candidate {candidateId = 10, codeReview = B, cultureFirt = C, education = MS}: False"
"Candidate {candidateId = 10, codeReview = B, cultureFirt = C, education = PhD}: False"
"Candidate {candidateId = 10, codeReview = B, cultureFirt = B, education = BA}: False"
"Candidate {candidateId = 10, codeReview = B, cultureFirt = B, education = MS}: False"
"Candidate {candidateId = 10, codeReview = B, cultureFirt = B, education = PhD}: False"
"Candidate {candidateId = 10, codeReview = B, cultureFirt = A, education = BA}: False"
"Candidate {candidateId = 10, codeReview = B, cultureFirt = A, education = MS}: False"
"Candidate {candidateId = 10, codeReview = B, cultureFirt = A, education = PhD}: False"
"Candidate {candidateId = 10, codeReview = A, cultureFirt = C, education = BA}: False"
"Candidate {candidateId = 10, codeReview = A, cultureFirt = C, education = MS}: False"
"Candidate {candidateId = 10, codeReview = A, cultureFirt = C, education = PhD}: False"
"Candidate {candidateId = 10, codeReview = A, cultureFirt = B, education = BA}: False"
"Candidate {candidateId = 10, codeReview = A, cultureFirt = B, education = MS}: True"
"Candidate {candidateId = 10, codeReview = A, cultureFirt = B, education = PhD}: True"
"Candidate {candidateId = 10, codeReview = A, cultureFirt = A, education = BA}: False"
"Candidate {candidateId = 10, codeReview = A, cultureFirt = A, education = MS}: True"
"Candidate {candidateId = 10, codeReview = A, cultureFirt = A, education = PhD}: True"
"Candidate {candidateId = 11, codeReview = B, cultureFirt = C, education = BA}: False"
"Candidate {candidateId = 11, codeReview = B, cultureFirt = C, education = MS}: False"
"Candidate {candidateId = 11, codeReview = B, cultureFirt = C, education = PhD}: False"
"Candidate {candidateId = 11, codeReview = B, cultureFirt = B, education = BA}: False"
"Candidate {candidateId = 11, codeReview = B, cultureFirt = B, education = MS}: False"
"Candidate {candidateId = 11, codeReview = B, cultureFirt = B, education = PhD}: False"
"Candidate {candidateId = 11, codeReview = B, cultureFirt = A, education = BA}: False"
"Candidate {candidateId = 11, codeReview = B, cultureFirt = A, education = MS}: False"
"Candidate {candidateId = 11, codeReview = B, cultureFirt = A, education = PhD}: False"
"Candidate {candidateId = 11, codeReview = A, cultureFirt = C, education = BA}: False"
"Candidate {candidateId = 11, codeReview = A, cultureFirt = C, education = MS}: False"
"Candidate {candidateId = 11, codeReview = A, cultureFirt = C, education = PhD}: False"
"Candidate {candidateId = 11, codeReview = A, cultureFirt = B, education = BA}: False"
"Candidate {candidateId = 11, codeReview = A, cultureFirt = B, education = MS}: True"
"Candidate {candidateId = 11, codeReview = A, cultureFirt = B, education = PhD}: True"
"Candidate {candidateId = 11, codeReview = A, cultureFirt = A, education = BA}: False"
"Candidate {candidateId = 11, codeReview = A, cultureFirt = A, education = MS}: True"
"Candidate {candidateId = 11, codeReview = A, cultureFirt = A, education = PhD}: True"
%