{- |
Description: Fibonacci numbers
Copyright: Copyright (C) 2021 Yoo Chung
License: GPL-3.0-or-later
Maintainer: dev@chungyc.org

Some solutions to "Problems.P29" of Ninety-Nine Haskell "Problems".
-}
module Solutions.P29 (fibonacci) where

{- |
Write a function to compute the \(n\)th Fibonacci number.
-}
fibonacci :: Integral a => a -> a
fibonacci :: forall a. Integral a => a -> a
fibonacci a
1 = a
1
fibonacci a
2 = a
1
fibonacci a
n | a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
2     = a -> a -> a -> a
forall a. Integral a => a -> a -> a -> a
fib a
1 a
1 a
n
            | Bool
otherwise = a
forall a. HasCallStack => a
undefined

fib :: Integral a => a -> a -> a -> a
fib :: forall a. Integral a => a -> a -> a -> a
fib a
k a
k' a
3 = a
k a -> a -> a
forall a. Num a => a -> a -> a
+ a
k'
fib a
k a
k' a
n = a -> a -> a -> a
forall a. Integral a => a -> a -> a -> a
fib a
k' (a
ka -> a -> a
forall a. Num a => a -> a -> a
+a
k') (a
na -> a -> a
forall a. Num a => a -> a -> a
-a
1)