class
Haskell は関数型言語。オブジェクト指向言語ではない。クラスって概念があるとはつゆ知らず。それに気付いたのは、つい先日。
はじめ、Char 同士の比較をするのに Char から Int に変換し、それを比較していた。時間が経つにつれ、変換して比較せずとも直接比較できることに気付いた。
なぜかというと、Char 型は Ord クラスのインスタンスだから。Ord はその名の通り順序に関するクラス。ほとんどの型が Ord クラスのインスタンスのようだ。
ただ、同じ Ord クラスのインスタンスでも型が違うとエラーが発生する。
Prelude> 'a' > 'b' False Prelude> 'a' > 10 <interactive>:1:6: No instance for (Num Char) arising from the literal `10' at <interactive>:1:6-7 Possible fix: add an instance declaration for (Num Char) In the second argument of `(>)', namely `10' In the expression: 'a' > 10 In the definition of `it': it = 'a' > 10
これから値を比較する際は、同じ型であれば不等号で比較すれば済むってことがわかった。