never executed always true always false
1 module HelVM.HelIO.Digit.Digits (
2 digitsToIntegral,
3 naturalToDigits7,
4 naturalToDigits2,
5 naturalToDigits,
6 ) where
7
8 import qualified Data.ListLike as LL
9 import HelVM.HelIO.Collections.SList
10 import HelVM.HelIO.Control.Safe
11
12 digitsToIntegral :: (MonadSafe m , Integral a) => a -> SList (m a) -> m a
13 digitsToIntegral base = foldr (liftedMulAndAdd base) (pure 0)
14
15 liftedMulAndAdd :: (MonadSafe m , Integral a) => a -> m a -> m a -> m a
16 liftedMulAndAdd base = liftA2 (mulAndAdd base)
17
18 mulAndAdd :: Integral a => a -> a -> a -> a
19 mulAndAdd base digit acc = acc * base + digit
20
21 ----
22
23 naturalToDigits7 :: Natural -> [Natural]
24 naturalToDigits7 = naturalToDigits 7
25
26 naturalToDigits2 :: Natural -> [Natural]
27 naturalToDigits2 = naturalToDigits 2
28
29 naturalToDigits :: Natural -> Natural -> [Natural]
30 naturalToDigits base = LL.reverse <$> unfoldr (modDivMaybe base)
31
32 modDivMaybe :: Natural -> Natural -> Maybe (Natural , Natural)
33 modDivMaybe _ 0 = Nothing
34 modDivMaybe base value = Just (swap $ value `divMod` base)