Converting number base

后端 未结 3 911
日久生厌
日久生厌 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 14:54

    Using the digits package from Hackage:

    import Data.Digits (digits, unDigits)
    
    convertBase :: Integral a => a -> a -> [a] -> [a]
    convertBase from to = digits to . unDigits from
    

    You can add a fromIntegral in there if you need the input and output types to be different. Also, the Integral constraint makes more sense than Num, since you probably don't want to deal with complex or floating-point digits.

提交回复
热议问题