Self-describing numbers: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: hlint, hindent – minor tidying)
(→‎{{header|Haskell}}: Minor consolidation of output into single pruned list)
Line 816: Line 816:
-- a base-n number are represented as a list of ints, one per digit
-- a base-n number are represented as a list of ints, one per digit
allBaseNNumsOfLength :: Int -> [[Int]]
allBaseNNumsOfLength :: Int -> [[Int]]
allBaseNNumsOfLength n = replicateM n [0..n-1]
allBaseNNumsOfLength = replicateM <*> (enumFromTo 0 . subtract 1)


isSelfDescribing :: [Int] -> Bool
isSelfDescribing :: [Int] -> Bool
isSelfDescribing num =
isSelfDescribing num = all (\(i, x) -> x == count i num) $ zip [0 ..] num
all (\(i,x) -> x == count i num) $ zip [0..] num


-- translate it back into an integer in base-10
-- translate it back into an integer in base-10
Line 826: Line 825:
decimalize = read . map intToDigit
decimalize = read . map intToDigit


main = forM_ [1..7] $
main :: IO ()
main =
print . map decimalize . filter isSelfDescribing . allBaseNNumsOfLength</lang>
(print . concat) $
map decimalize . filter isSelfDescribing . allBaseNNumsOfLength <$> [1 .. 7]</lang>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==