As far as I can see, this code snippet should compile without a problem:
import Data.Digits (digits)
-- |convert integer to arbitrary base with specified charset
-- base/radix is charset string length.
-- eg. convert the integer 255 to hex:
-- intToBaseN 255 "0123456789abcdef" = "ff"
numToBaseN :: Integral n => n -> [Char] -> String
numToBaseN num charlst = map (\i -> charlst !! (fromIntegral i)) lst where
lst = digits (length charlst) num
But GHC complains that the num
in the lst
expression isn't an Int
. But the type of digits
is
digits :: Integral n => n -> n -> [n]
it doesn't require an Int
as an argument, only something that is an Integral, which the type signature of numToBaseN
does as well.
!!
requires an Int, which is why it is converted using fromIntegral
.
What's going on here?
It compiles if I replace num
with (fromIntegral num)
, but then I lose the ability to convert an Integer (ie. arbitrarily large integers).
Both arguments to digits
need to have the same type and length charlst
has the type Int
, so num
must also have type Int
.
It compiles if I replace num with (fromIntegral num), but then I lose the ability to convert an Integer
If you apply fromIntegral
to length charlst
instead, it will convert it to whichever type num
is, so it'll work the way you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With