
27 Sep
2007
27 Sep
'07
11:11 p.m.
If you don't like explicit recursion (or points):
intToBin = map (`mod` 2) . takeWhile (>0) . iterate (`div` 2)
binToInt = foldl' (\n d -> n*2+d) 0
or even:
binToInt = foldl' ((+).(*2)) 0
On 27/09/2007, 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
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe