計算機科学のブログ

型によるプログラミング Maybe型:欠損値に対処する Maybe:型を使って欠損値に対処する JustとNothing

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT3(型によるプログラミング)、LESSON 19(Maybe型:欠損値に対処する)、19.1(Maybe:型を使って欠損値に対処する)、クイックチェック 19-1の解答を求めてみる。

Maybe Organ型。

実際に確認。

lesson/package.yaml

name:                lesson
version:             0.1.0.0
github:              "githubuser/lesson"
license:             BSD3
author:              "Author name here"
maintainer:          "example@example.com"
copyright:           "2022 Author name here"

extra-source-files:
- README.md
- ChangeLog.md

# Metadata used when publishing your package
# synopsis:            Short description of your package
# category:            Web

# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description:         Please see the README on GitHub at <https://github.com/githubuser/lesson#readme>

dependencies:
- base >= 4.7 && < 5
- containers

library:
  source-dirs: src

executables:
  lesson-exe:
    main:                Main.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - lesson

tests:
  lesson-test:
    main:                Spec.hs
    source-dirs:         test
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - lesson

コード

lesson/app/Main.hs

module Main where

import qualified Data.Map as Map
import Lib

main :: IO ()
main = do
  print $ Map.lookup 6 organCatalog

lesson/src/Lib.hs

module Lib where

import qualified Data.Map as Map

data Organ
  = Heart
  | Brain
  | Kidney
  | Spleen
  deriving (Show)

organs :: [Organ]
organs = [Heart, Heart, Brain, Spleen, Spleen, Kidney]

ids :: [Int]
ids = [2, 7, 13, 14, 21, 24]

organPairs :: [(Int, Organ)]
organPairs = zip ids organs

organCatalog :: Map.Map Int Organ
organCatalog = Map.fromList organPairs

入出力結果(Terminal, Zsh)

% stack exec lesson-exe
Nothing
% stack ghci
Using main module: 1. Package `lesson' component lesson:exe:lesson-exe with main-is file: /Users/…/lesson/app/Main.hs
The following GHC options are incompatible with GHCi and have not been passed to it: -threaded
Configuring GHCi with the following packages: lesson

* * * * * * * *

Warning: Multiple files use the same module name:
         * Paths_lesson found at the following paths
           * /Users/…/lesson/.stack-work/dist/x86_64-osx/Cabal-3.4.1.0/build/autogen/Paths_lesson.hs (lesson:lib)
           * /Users/…/lesson/.stack-work/dist/x86_64-osx/Cabal-3.4.1.0/build/lesson-exe/autogen/Paths_lesson.hs (lesson:exe:lesson-exe)
* * * * * * * *

GHCi, version 9.0.2: https://www.haskell.org/ghc/  :? for help
macro 'doc' overwrites builtin command.  Use ':def!' to overwrite.
(0.00 secs, 0 bytes)
(0.00 secs, 0 bytes)
Loaded GHCi configuration from /Users/…/.ghc/ghci.conf
[1 of 3] Compiling Lib              ( /Users/…/lesson/src/Lib.hs, interpreted )
[2 of 3] Compiling Main             ( /Users/…/lesson/app/Main.hs, interpreted )
[3 of 3] Compiling Paths_lesson     ( /Users/…/lesson/.stack-work/dist/x86_64-osx/Cabal-3.4.1.0/build/autogen/Paths_lesson.hs, interpreted )
Ok, three modules loaded.
Loaded GHCi configuration from /private/var/folders/wj/52wjq8dn0fj1qltsnx3xxpnh0000gn/T/haskell-stack-ghci/5d8024c5/ghci-script
*Main Lib Paths_lesson
λ> Map.lookup 6 organ
organCatalog  organPairs    organs
λ> Map.lookup 6 organCatalog 
Nothing
it :: Maybe Organ
(0.02 secs, 518,576 bytes)
*Main Lib Paths_lesson
λ> :t Map.lookup 6 organCatalog 
Map.lookup 6 organCatalog :: Maybe Organ
*Main Lib Paths_lesson
λ> :quit 
Leaving GHCi.
%