Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,4 @@
Prelude> Numeric.showIntAtBase 16 Char.intToDigit 42 ""
"2a"
Prelude> fst $ head $ Numeric.readInt 16 Char.isHexDigit Char.digitToInt "2a"
42

View file

@ -0,0 +1,21 @@
import Data.List
import Data.Char
toBase :: Int -> Int -> [Int]
toBase b v = toBase' [] v where
toBase' a 0 = a
toBase' a v = toBase' (r:a) q where (q,r) = v `divMod` b
fromBase :: Int -> [Int] -> Int
fromBase b ds = foldl' (\n k -> n * b + k) 0 ds
toAlphaDigits :: [Int] -> String
toAlphaDigits = map convert where
convert n | n < 10 = chr (n + ord '0')
| otherwise = chr (n + ord 'a' - 10)
fromAlphaDigits :: String -> [Int]
fromAlphaDigits = map convert where
convert c | isDigit c = ord c - ord '0'
| isUpper c = ord c - ord 'A' + 10
| isLower c = ord c - ord 'a' + 10

View file

@ -0,0 +1,4 @@
*Main> toAlphaDigits $ toBase 16 $ 42
"2a"
*Main> fromBase 16 $ fromAlphaDigits $ "2a"
42

View file

@ -0,0 +1,54 @@
import Data.Bifunctor (first)
import Data.List (unfoldr)
import Data.Tuple (swap)
import Data.Bool (bool)
inBaseDigits :: String -> Int -> String
inBaseDigits ds n =
let base = length ds
in reverse $
unfoldr
((<*>)
(bool Nothing . Just . first (ds !!) . swap . flip quotRem base)
(0 <))
n
inLowerHex :: Int -> String
inLowerHex = inBaseDigits "0123456789abcdef"
inUpperHex :: Int -> String
inUpperHex = inBaseDigits "0123456789ABCDEF"
inBinary :: Int -> String
inBinary = inBaseDigits "01"
inOctal :: Int -> String
inOctal = inBaseDigits "01234567"
inDevanagariDecimal :: Int -> String
inDevanagariDecimal = inBaseDigits "०१२३४५६७८९"
inHinduArabicDecimal :: Int -> String
inHinduArabicDecimal = inBaseDigits "٠١٢٣٤٥٦٧٨٩"
toBase :: Int -> Int -> String
toBase intBase n
| (intBase < 36) && (intBase > 0) =
inBaseDigits (take intBase (['0' .. '9'] ++ ['a' .. 'z'])) n
| otherwise = []
main :: IO ()
main =
mapM_ putStrLn $
[ inLowerHex
, inUpperHex
, inBinary
, inOctal
, toBase 16
, toBase 2
, inDevanagariDecimal
, inHinduArabicDecimal
] <*>
[254]