summaryrefslogtreecommitdiff
path: root/template/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2021-07-19 10:06:45 +0000
committerFuwn <[email protected]>2021-07-19 10:06:45 +0000
commitcd3647db7f365fd040f7e8248681bbc5f01030b8 (patch)
treeac4451382aa7db8bd9c700daa5a0c6ee1774926b /template/src
downloadhaskell-stack-template-cd3647db7f365fd040f7e8248681bbc5f01030b8.tar.xz
haskell-stack-template-cd3647db7f365fd040f7e8248681bbc5f01030b8.zip
feat(hst): :star:HEADmaster
Diffstat (limited to 'template/src')
-rw-r--r--template/src/exe/Main.hs15
-rw-r--r--template/src/lib/Template.hs19
2 files changed, 34 insertions, 0 deletions
diff --git a/template/src/exe/Main.hs b/template/src/exe/Main.hs
new file mode 100644
index 0000000..a174244
--- /dev/null
+++ b/template/src/exe/Main.hs
@@ -0,0 +1,15 @@
+{-# LANGUAGE ScopedTypeVariables #-}
+
+module Main where
+
+import System.Environment ( getArgs )
+import Template ( fib )
+
+main :: IO ()
+main = do
+ -- https://lotz84.github.io/haskellbyexample/ex/command-line-arguments
+ args <- getArgs
+ let arg = head args
+
+ -- https://stackoverflow.com/a/20667721
+ print (fib (read arg :: Int))
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' -- '