
27 Sep
2007
27 Sep
'07
10:30 p.m.
On 9/27/07, PR Stanley
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
IntToBin diverges for inputs <= 0. You could get 0 "for free" with intToBin :: Int -> [Int] intToBin 0 = [] intToBin n = (intToBin (n`div`2)) ++ [n `mod` 2] And why not use [Bool] for the "Bin" type? Or data Bin = Zero | One Regards, Chris