2

In the Numeric module, I find the following function showIntAtBase. Could I use for convert a decimal number into a binary or octal number?

Does this function work like this?

showIntAtBase 6 1025554775 =  245433110435
showIntAtBase 2 5264564526752765267240006 = 10001011010110100001001110101100111000111110101011111100100100001010110010001000110

If it doesn't work this way, can you tell me any predefined ones to do this?

duplode
  • 33,731
  • 7
  • 79
  • 150
  • Here's [an existing good solution to the "convert a number's base" problem](https://stackoverflow.com/a/10028866/). – hnefatl Jan 29 '18 at 18:52

1 Answers1

2

It is not clear what you are asking:

Does this function work like this?

showIntAtBase 6 1025554775 =  245433110435
  1. No. The function defined in Numeric does not work by pattern matching all possible numbers and providing a literal result.
  2. No. The code you typed is not type correct. You haven't even provided the correct number of arguments.

Notice the type for showIntAtBase:

> :t showIntAtBase
showIntAtBase
  :: (Show a, Integral a) => a -> (Int -> Char) -> a -> ShowS

Expanding for the ShowS type alias we get:

> :t showIntAtBase
showIntAtBase
  :: (Show a, Integral a) => a -> (Int -> Char) -> a -> String -> String

So you need to provide four arguments, not two numbers. For example, you can use Data.Char.intToDigit to convert integers to characters and you can use the empty string for the suffix string:

> import Data.Char (intToDigit)
> import Numeric (showIntAtBase)
> showIntAtBase 6 intToDigit 1025554775 ""
"245433110435"
ggorlen
  • 44,755
  • 7
  • 76
  • 106
Thomas M. DuBuisson
  • 64,245
  • 7
  • 109
  • 166