Non-decimal radices/Output: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: (removed redundant import))
(→‎{{header|Haskell}}: (a less partial draft of intToDigits))
Line 577: Line 577:
-- ARBITRARY RADICES ----------------------------------------------------------
-- ARBITRARY RADICES ----------------------------------------------------------
bases :: [Int]
bases :: [Int]
bases = [2, 7, 8, 10, 12, 16]
bases = abs <$> [2, 7, 8, 10, 12, 16]


tableRows :: [[String]]
tableRows :: [[String]]
Line 584: Line 584:
intToDigits :: Int -> Int -> String
intToDigits :: Int -> Int -> String
intToDigits base n =
intToDigits base n =
let ds = take base (['0' .. '9'] <> ['A' .. 'Z'])
let b = abs base
ds = take b (['0' .. '9'] <> ['A' .. 'Z'])
in reverse $
unfoldr
in if b <= 36
(\x ->
then reverse $
if x > 0
unfoldr
then let (q, r) = quotRem x base
(\x ->
in Just (ds !! r, q)
if x > 0
else Nothing)
then let (q, r) = quotRem x b
in Just (ds !! r, q)
n
else Nothing)
n
else "Needs numeric glyphs beyond Z"


-- TEST AND TABULATION---------------------------------------------------------
-- TEST AND TABULATION---------------------------------------------------------