RosettaCodeData/Task/Self-describing-numbers/Haskell/self-describing-numbers-2.hs
2018-06-22 20:57:24 +00:00

22 lines
696 B
Haskell

import Data.Char (intToDigit)
import Control.Monad (replicateM, forM_)
count :: Int -> [Int] -> Int
count x = length . filter (x ==)
-- all the combinations of n digits of base n
-- a base-n number are represented as a list of ints, one per digit
allBaseNNumsOfLength :: Int -> [[Int]]
allBaseNNumsOfLength = replicateM <*> (enumFromTo 0 . subtract 1)
isSelfDescribing :: [Int] -> Bool
isSelfDescribing num = all (\(i, x) -> x == count i num) $ zip [0 ..] num
-- translate it back into an integer in base-10
decimalize :: [Int] -> Int
decimalize = read . map intToDigit
main :: IO ()
main =
(print . concat) $
map decimalize . filter isSelfDescribing . allBaseNNumsOfLength <$> [1 .. 8]