2018-01-05から1日間の記事一覧

Haskellの型クラスを利用した有理数型

タイトル通りです。 data Ration = Integer :/ Integer | AddID instance Eq Ration where (x :/ y) == (x':/y') = let s1 = gcd x y s2 = gcd x' y' in (x `div` s1 == x' `div` s2) && (y `div` s1 == y' `div` s2) instance Show Ration where show (x :/…

高速フィボナッチ

Haskellで書きました。 matrix2 (x1,y1,z1,w1) (x2,y2,z2,w2) = (x2*x1+y1*z2 , x1*y2+y1*w2,x2*z1+w1*z2,y2*z1 + w1*w2) repeat' n f x y | n < 1 = f x y | n == 1 = f x y | n > 1 = repeat' (n-1) f (f x y) y third (x,y,z,w) = z matrxpow m n | n == …