Created
July 17, 2020 14:32
-
-
Save abiodun0/fbfdcd2c3525987602fd26a26dd807d5 to your computer and use it in GitHub Desktop.
Fib with monodis, 25 days back
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Matrices multiplication are associative by default, hence the use of semigroup and monoids in this case | |
import Data.Semigroup | |
data Matrix2x2 = Matrix | |
{ x00 :: Integer, x01 :: Integer | |
, x10 :: Integer, x11 :: Integer | |
} | |
instance Monoid Matrix2x2 where | |
mempty = | |
Matrix | |
{ x00 = 1, x01 = 0 | |
, x10 = 0, x11 = 1 | |
} | |
instance Semigroup Matrix2x2 where | |
Matrix l00 l01 l10 l11 <> Matrix r00 r01 r10 r11 = | |
Matrix | |
{ x00 = l00 * r00 + l01 * r10, x01 = l00 * r01 + l01 * r11 | |
, x10 = l10 * r00 + l11 * r10, x11 = l10 * r01 + l11 * r11 | |
} | |
f :: Integer -> Integer | |
f n = x01 (mtimesDefault n matrix) | |
where | |
matrix = | |
Matrix | |
{ x00 = 0, x01 = 1 | |
, x10 = 1, x11 = 1 | |
} | |
main = do | |
putStrLn $ show $ f 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment