Converting number base

后端 未结 3 913
日久生厌
日久生厌 2021-02-08 14:29

Is there a platform function that will do the following?

convertBase :: (Num a, Num b) => Int -> Int -> [a] -> [b]

Convert a number

3条回答
  •  粉色の甜心
    2021-02-08 15:09

    The closest thing in the haskell platform is from module Numeric:

    readInt :: Num a => a -> (Char -> Bool) -> (Char -> Int) -> ReadS a
    showIntAtBase :: Integral a => a -> (Int -> Char) -> a -> ShowS

    fromBase :: Int -> String -> Int
    fromBase base = fst . head . readInt base (( Int -> String
    toBase base num = showIntAtBase base intToDigit num ""
    
    fromBaseToBase :: Int -> Int -> String -> String
    fromBaseToBase from to = toBase to . fromBase from
    

提交回复
热议问题