class Show a where
show :: a -> String
instance Show String where
show :: String -> String
show = id
> show "Type Class" --> "Type Class"
メンバ関数を具体的に実装することで
StringがShow型クラスに属する証拠を与える
Show 型クラスのインスタンス2
class Show a where
show :: a -> String
instance Show String where
show = id
instance (Show a) => Show [a] where
show [] = ""
show [x] = show x
show (x:xs) = show x ++ ", " ++ show xs
> show ["Hello", "World"] --> "Hello, World"
Show型クラスの性質を再帰的に表現できる
Exercise
型 (a, b) を Show 型クラスの
インスタンスにせよ
Show 型クラスのインスタンス3
instance (Show a, Show b) => Show (a, b) where
show (x, y) = "(" ++ show x ++ ", " ++ show y ++ ")"
> show ("hoge", "fuga") --> "(hoge, fuga)"