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 |
|||
⚫ | |||
intToDigits :: Int -> Int -> String |
intToDigits :: Int -> Int -> String |
||
intToDigits base n = |
intToDigits base n = |
||
let b = abs base |
let b = abs base |
||
⚫ | |||
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 ( |
in Just (digits ! r, q) |
||
else Nothing) |
else Nothing) |
||
n |
n |