Skip to content

Instantly share code, notes, and snippets.

@KatagiriSo
Created June 12, 2017 09:41
Show Gist options
  • Save KatagiriSo/981ff6d9aee158ff2dd6810a3ac6c7e8 to your computer and use it in GitHub Desktop.
Save KatagiriSo/981ff6d9aee158ff2dd6810a3ac6c7e8 to your computer and use it in GitHub Desktop.
-- 型クラスの練習
import Data.Set
-- aをNumに制限Numのインスタンスにしばる
mySum :: Num a => [a] -> a
mySum list = list !! 0
data Point = Pt Int Int deriving Show
instance Eq Point where
(Pt x y) == (Pt x' y') = x==x' && y==y'
-- Containerはcmap関数を持つとする
class Container c where
cmap :: (a -> b) -> c a -> c b
-- MaybeはContainerクラスのインスタンスである
-- cmap :: (a->b) -> Maybe a -> Maybe b
instance Container Maybe where
cmap f Nothing = Nothing
cmap f (Just x) = Just (f x)
-- http://d.hatena.ne.jp/kazu-yamamoto/20091116/1258357568
data Modulo = Mod Integer Integer deriving (Eq, Show, Ord)
instance Num Modulo where
(Mod n1 m1) * (Mod n2 m2)
| m1 == m2 = Mod (n1 * n2 `mod` m1) m1
| otherwise = error "module mismatch"
myIdeal x m = fromList [Mod x m * Mod n m | n <-[0..(m-1)]]
myIdeals m = fromList [myIdeal x m| x <- [0..m]]
-- つまり Z/6Z
-- {0},{0,2,4},{0,3},{0,1,2,3,4,5}
-- 局所化
data Fraction = Frac Modulo Modulo deriving (Eq, Show)
instance Num Fraction where
(Frac n1 s1) * (Frac n2 s2)
| s1 != 0 && s2 != 0 = Frac (n1*s2 + n2*s1) (s1*s2)
| otherwise = error "mismatch"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment