Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert tic xenotation to 1-2 digits and back
module SmallNumbers where
-- http://www.xenosystems.net/360-small-numbers/
-- 12 -> 0
-- 1122 -> (0) -- 111222 -> ((0))
-- 112122 -> (00) -- 11122122 -> ((0)0)
-- 11212122 -> (000) -- 1112212122 -> ((0)00)
asInteger :: String -> Integer
asInteger n = read n :: Integer
nlist :: Integer -> [Integer]
nlist n = (asInteger . (:[])) <$> (show n)
-- convert 1-2 digits to xenotation
xeno' :: [Integer] -> [Char]
xeno' [] = []
xeno' [x] = [] -- always even # of digits; last digit always 2
xeno' (x:y:xs)
| (x == 1) && (y == 1) = '(' : xeno' (y:xs)
| (x == 2) && (y == 2) = ')' : xeno' (y:xs)
| (x == 1) && (y == 2) = '0' : xeno' (y:xs)
| otherwise = xeno' (y:xs) -- skip 2:1
xeno n = xeno' (nlist n)
--convert xenotation to 1-2 digits
fromXeno' :: Num a => [Char] -> [a]
fromXeno' "" = []
fromXeno' (x:xs)
| (x == '(') = 1 : fromXeno' xs
| (x == ')') = 2 : fromXeno' xs
| otherwise = 12 : fromXeno' xs -- can use 0 or . notation
fromXeno s = asInteger . concat $ show <$> (fromXeno' s)
{- EXAMPLES
The function xeno expects an integer, like so:
xeno 1112212122
Conversely, fromXeno expects a string, so the xenotation should be enclosed in quotation marks.
fromXeno "((0)00)"
To test equivalence, we can do one of the following, which will give back the original input.
xeno $ fromXeno "((0)00)"
fromXeno $ xeno 1112212122
I use the Integer type because Int sometimes 'simplifies' numbers with too many digits, causing errors.
For simplicity, I haven't included tests for nonsense inputs.
Also, TX-equivalent numbers such as ((0)0)0 and 0(0(0)) will give different 1-2 representations.
A possible extension is to allow other types of digits besides '1' and '2', e.g. 1 and 0.
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.