Non-decimal radices/Output: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: (added a further radix (32) to the test))
m (→‎{{header|Haskell}}: (updated baseDigits function))
Line 581: Line 581:


tableRows :: [[String]]
tableRows :: [[String]]
tableRows = ((([intToDigits] <*> bases) <*>) . return) <$> [1 .. 33]
tableRows = ((([baseDigits] <*> bases) <*>) . return) <$> [1 .. 33]


digits :: Array Int Char
digits :: Array Int Char
digits = listArray (0, 35) (['0' .. '9'] <> ['A' .. 'Z'])
digits = listArray (0, 35) (['0' .. '9'] <> ['A' .. 'Z'])


intToDigits :: Int -> Int -> String
baseDigits :: Int -> Int -> String
intToDigits base n =
baseDigits base
| base > 36 = const "Needs glyphs beyond Z"
let b = abs base
| otherwise = reverse . unfoldr remQuot
in if b <= 36
where
then reverse $
unfoldr
remQuot 0 = Nothing
(\x ->
remQuot n =
if x > 0
let (q, r) = quotRem n base
in Just (digits ! r, q)
then let (q, r) = quotRem x b
in Just (digits ! r, q)
else Nothing)
n
else "Needs numeric glyphs beyond Z"


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