wvogel日記

自分用の技術備忘録が多めです.

型Class

今日は型クラスの勉強
というわけで。
図書館で見つけた本を参考に
The Haskell School of Expression: Learning Functional Programming through Multimedia

module ClassTest where

import Prelude hiding ((==),(/=))

class MyEq a where
  (==),(/=) :: a -> a -> Bool
  a == b = not $ a /= b
  a /= b = not $ a == b

instance MyEq Integer where
  x == y = case y-x of
            0 -> True
            otherwise -> False

instance MyEq Int where
  x == y = case y-x of
            0 -> True
            otherwise -> False

有名なEqクラス、自分で書いてみた。
まずはInteger,Intに対して。
String(Char)での定義はどうやったらできるんだろう.....


にしてもこの本結構面白い
読みやすいし
さて、明日も授業なのでもう寝ます