Jump to content

Non-decimal radices/Convert: Difference between revisions

m
m (→‎{{header|Haskell}}: Minor tidying)
Line 1,378:
If we want to assume a default character set, then a general '''toBase''' (Int -> Int -> String) can be also be derived from '''inBaseDigits'''.
 
<lang haskell>import DataControl.CharArrow (intToDigitfirst)
import Data.Char (intToDigit)
import Data.List (unfoldr)
import Data.Tuple (swap)
 
inBaseDigits :: String -> Int -> String
inBaseDigits ds n = reverse $ unfoldr go n
where
base = length ds
go x
| 0 < x = Just $ first (ds !!) (swap $ quotRem x (length ds))
let (d, r) = quotRem x base
in Just (ds !! r, d)
| otherwise = Nothing
 
9,655

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.