Non-decimal radices/Output: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: (a less partial draft of intToDigits))
(→‎{{header|Haskell}}: (used Array Int Char for digits, rather than [Char]))
Line 573: Line 573:
Or, generalising and tabulating a little:
Or, generalising and tabulating a little:
<lang haskell>import Data.Monoid ((<>))
<lang haskell>import Data.Monoid ((<>))
import Data.Array (Array, listArray, (!))
import Data.List (unfoldr, transpose, intercalate)
import Data.List (unfoldr, transpose, intercalate)


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

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


intToDigits :: Int -> Int -> String
intToDigits :: Int -> Int -> String
intToDigits base n =
intToDigits base n =
let b = abs base
let b = abs base
ds = take b (['0' .. '9'] <> ['A' .. 'Z'])
in if b <= 36
in if b <= 36
then reverse $
then reverse $
Line 592: Line 595:
if x > 0
if x > 0
then let (q, r) = quotRem x b
then let (q, r) = quotRem x b
in Just (ds !! r, q)
in Just (digits ! r, q)
else Nothing)
else Nothing)
n
n