import Control.Applicative import Data.Bits import Data.Char import Data.List import Data.Tuple main :: IO () main = mapM_ putStrLn $ [f534, f534', g534] <*> [0..10] f534 :: Integral a => a -> String f534 = flip (map . (((intToDigit . signum) .) . (.&.) . fromIntegral)) =<< takeWhile (not . (== 0)) . iterate (`div` 2) . bit . floorLogBase2FromIntegral . (if' id (const nnErr) =<< (>= 0)) f534' :: Integral a => a -> String f534' = ((map intToDigitFromIntegral . snd) .) . flip (mapAccumR divMod) =<< flip replicate 2 . succ . floorLogBase2FromIntegral . (if' id (const nnErr) =<< (>= 0)) g534 :: Integral a => a -> String g534 = if' (const "0") (map intToDigitFromIntegral . reverse . unfoldr (if' (const Nothing) (Just . swap . (`divMod` 2)) =<< (== 0)) . (if' id (const nnErr) =<< (>= 0))) =<< (== 0) nnErr :: e nnErr = error "applied to negative number" floorLogBase2FromIntegral :: (Integral a, Integral b) => a -> b floorLogBase2FromIntegral = floor . logBase 2 . fromIntegral intToDigitFromIntegral :: Integral a => a -> Char intToDigitFromIntegral = intToDigit . fromIntegral if' :: a -> a -> Bool -> a if' f g p = if p then f else g