-------------------- -- Base conversions for numerals unfoldl p f a x = if p x then a else unfoldl p f (y:a) r where (r,y) = f x sumProd b n m = n * b + m contract b = foldl (sumProd b) 0 expand b = unfoldl (==0) (`divMod` b) [] number b = contract b . map digitToInt string b = map intToDigit . expand b [ (bin, unbin), (dec, undec), (hex, unhex) ] = map base [2, 10, 16] where base b = (string b, number b) -------------------- -- unfoldr from the List module unfoldr f b = case f b of Nothing -> [] Just (a,b') -> a : unfoldr f b' unfold :: (b -> Bool) -> (b -> a) -> (b -> b) -> b -> [a] unfold p h g b = if p b then h b : unfold p h g (g b) else []