module HMath.Basic (
factorial,
fibonacci,
isPrime,
clamp,
lerp,
) where
factorial :: Integer -> Integer
factorial :: Integer -> Integer
factorial Integer
n
| Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 = [Char] -> Integer
forall a. HasCallStack => [Char] -> a
error [Char]
"factorial: negative input"
| Bool
otherwise = [Integer] -> Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product [Integer
1 .. Integer
n]
fibonacci :: Integer -> Integer
fibonacci :: Integer -> Integer
fibonacci Integer
n
| Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 = [Char] -> Integer
forall a. HasCallStack => [Char] -> a
error [Char]
"fibonacci: negative input"
| Bool
otherwise = Integer -> Integer -> Integer -> Integer
forall {t} {t}. (Eq t, Num t, Num t) => t -> t -> t -> t
go Integer
n Integer
0 Integer
1
where
go :: t -> t -> t -> t
go t
0 t
a t
_ = t
a
go t
k t
a t
b = t -> t -> t -> t
go (t
k t -> t -> t
forall a. Num a => a -> a -> a
- t
1) t
b (t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
b)
isPrime :: Integer -> Bool
isPrime :: Integer -> Bool
isPrime Integer
n
| Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
2 = Bool
False
| Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
2 = Bool
True
| Integer -> Bool
forall a. Integral a => a -> Bool
even Integer
n = Bool
False
| Bool
otherwise = Integer -> Bool
check Integer
3
where
check :: Integer -> Bool
check Integer
d
| Integer
d Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
n Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
d = Bool
True
| Integer
n Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
d Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 = Bool
False
| Bool
otherwise = Integer -> Bool
check (Integer
d Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
2)
clamp :: (Ord a) => a -> a -> a -> a
clamp :: forall a. Ord a => a -> a -> a -> a
clamp a
low a
high a
value = a -> a -> a
forall a. Ord a => a -> a -> a
max a
low (a -> a -> a
forall a. Ord a => a -> a -> a
min a
high a
value)
lerp :: (Fractional a) => a -> a -> a -> a
lerp :: forall a. Fractional a => a -> a -> a -> a
lerp a
a a
b a
t = a
a a -> a -> a
forall a. Num a => a -> a -> a
+ (a
b a -> a -> a
forall a. Num a => a -> a -> a
- a
a) a -> a -> a
forall a. Num a => a -> a -> a
* a
t