RosettaCodeData/Task/Variable-length-quantity/Haskell/variable-length-quantity-2.hs

20 lines
339 B
Haskell
Raw Permalink Normal View History

2017-09-23 10:01:46 +02:00
import Data.List (intercalate)
base :: Int
2013-10-27 22:24:23 +00:00
base = 8
2017-09-23 10:01:46 +02:00
to :: Int -> [Int]
2013-10-27 22:24:23 +00:00
to 0 = []
to i = to (div i base) ++ [mod i base]
2017-09-23 10:01:46 +02:00
from :: [Int] -> Int
from = foldl1 ((+) . (base *))
2013-10-27 22:24:23 +00:00
2017-09-23 10:01:46 +02:00
main :: IO ()
main =
mapM_
(putStrLn .
intercalate " <-> " .
(((:) . concatMap show . to) <*> (return . show . from . to)))
[2097152, 2097151]