Last active
August 29, 2015 14:08
-
-
Save briansunter/2bbcff3da249288d8127 to your computer and use it in GitHub Desktop.
Convert Base 10 to Base 1.1 Million
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Base10M | |
(encode | |
,decode) where | |
import Data.List(elemIndex) | |
import Data.Maybe(fromJust) | |
unicodes = ['a'..] | |
numOfUnicodes = toInteger $ length unicodes | |
numToDigs :: Integer -> Integer -> [Integer] | |
numToDigs 0 _ = [] | |
numToDigs x b = numToDigs (x `div` b) b ++ [x `mod` b] | |
digsToNum :: [Integer] -> Integer | |
digsToNum [] = 0 | |
digsToNum (x:xs) = x * (numOfUnicodes ^ (length xs)) + digsToNum xs | |
numToUni :: Integer -> Char | |
numToUni x = unicodes !! fromInteger x | |
uniToNum :: Char -> Integer | |
uniToNum x = toInteger $ fromJust $ elemIndex x unicodes | |
encode :: Integer -> [Char] | |
encode x = map numToUni $ numToDigs x numOfUnicodes | |
decode :: [Char] -> Integer | |
decode x = digsToNum $ map uniToNum x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment