summaryrefslogtreecommitdiff
path: root/template/src/lib/Template.hs
diff options
context:
space:
mode:
Diffstat (limited to 'template/src/lib/Template.hs')
-rw-r--r--template/src/lib/Template.hs19
1 files changed, 19 insertions, 0 deletions
diff --git a/template/src/lib/Template.hs b/template/src/lib/Template.hs
new file mode 100644
index 0000000..29103b2
--- /dev/null
+++ b/template/src/lib/Template.hs
@@ -0,0 +1,19 @@
+module Template
+ ( fib
+ ) where
+
+import Data.Bits
+import Data.List
+
+-- https://wiki.haskell.org/The_Fibonacci_sequence
+fib :: Int -> Integer
+fib n =
+ snd
+ . foldl_ fib_ (1, 0)
+ . dropWhile not
+ $ [ testBit n k | k <- let s = bitSize n in [s - 1, s - 2 .. 0] ]
+ where
+ fib_ (f, g) p | p = (f * (f + 2 * g), ss)
+ | otherwise = (ss, g * (2 * f - g))
+ where ss = f * f + g * g
+ foldl_ = foldl' -- '