2 Module : Gargantext.Prelude.Utils
3 Description : Useful Tools near Prelude of the project
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Nice optimization of the Fibonacci function.
13 Gabriel Gonzales, Blazing fast Fibonacci numbers using Monoids, 2020-04,
14 http://www.haskellforall.com/2020/04/blazing-fast-fibonacci-numbers-using.html
15 (This post illustrates a nifty application of Haskell’s standard library to solve a numeric problem.)
23 module Gargantext.Prelude.Fibonacci where
25 import qualified Data.Semigroup as Semigroup
27 -------------------------------------------------------------
28 fib' :: Integer -> Integer
31 fib' n = fib (n-1) + fib (n-2)
32 -------------------------------------------------------------
35 data Matrix2x2 = Matrix
36 { x00 :: Integer, x01 :: Integer
37 , x10 :: Integer, x11 :: Integer
40 instance Monoid Matrix2x2 where
47 instance Semigroup Matrix2x2 where
48 Matrix l00 l01 l10 l11 <> Matrix r00 r01 r10 r11 =
50 { x00 = l00 * r00 + l01 * r10, x01 = l00 * r01 + l01 * r11
51 , x10 = l10 * r00 + l11 * r10, x11 = l10 * r01 + l11 * r11
54 fib :: Integer -> Integer
55 fib n = x01 (Semigroup.mtimesDefault n matrix)