型の紹介 型クラスを使用する Showを実装する
入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT2(型の紹介)、LESSON 14(型クラスを使用する)、14.2(Showを実装する)、クイックチェック 14-1Qの解答を求めてみる。
コード
lesson/app/Main.hs
module Main where
import Lib (sides)
main :: IO ()
main = do
mapM_ print sides
lesson/src/Lib.hs
module Lib
( sides,
)
where
sides :: [SixSideDie]
sides = [S1, S2, S3, S4, S5, S6]
data SixSideDie
= S1
| S2
| S3
| S4
| S5
| S6
instance Show SixSideDie where
show S1 = "I"
show S2 = "II"
show S3 = "Ⅲ"
show S4 = "Ⅳ"
show S5 = "Ⅴ"
show S6 = "Ⅵ"
入出力結果(Terminal, Zsh)
% stack build
lesson-0.1.0.0: unregistering (local file changes: app/Main.hs)
lesson> build (lib + exe)
Preprocessing library for lesson-0.1.0.0..
Building library for lesson-0.1.0.0..
Preprocessing executable 'lesson-exe' for lesson-0.1.0.0..
Building executable 'lesson-exe' for lesson-0.1.0.0..
[2 of 2] Compiling Main
Linking .stack-work/dist/x86_64-osx/Cabal-3.4.1.0/build/lesson-exe/lesson-exe ...
lesson> copy/register
Installing library in /Users/…/lesson/.stack-work/install/x86_64-osx/11bee7f1432551708036aee298c43f57d1e8b1a145053e7d2d3b07e2f4ae638f/9.0.2/lib/x86_64-osx-ghc-9.0.2/lesson-0.1.0.0-CIOIkpTgcfQI9akDJnwnSd
Installing executable lesson-exe in /Users/…/lesson/.stack-work/install/x86_64-osx/11bee7f1432551708036aee298c43f57d1e8b1a145053e7d2d3b07e2f4ae638f/9.0.2/bin
Registering library for lesson-0.1.0.0..
% stack exec lesson-exe
I
II
Ⅲ
Ⅳ
Ⅴ
Ⅵ
%