[Haskell-cafe] int to bin, bin to int

PR Stanley prstanley at ntlworld.com
Thu Sep 27 18:07:32 EDT 2007


Hi
intToBin :: Int -> [Int]
intToBin 1 = [1]
intToBin n = (intToBin (n`div`2)) ++ [n `mod` 2]

binToInt :: [Integer] -> Integer
binToInt [] = 0
binToInt (x:xs) = (x*2^(length xs)) + (binToInt xs)
Any comments and/or criticisms on the above definitions would be appreciated.
Thanks , Paul



More information about the Haskell-Cafe mailing list