Munchausen numbers: Difference between revisions
→{{header|Haskell}}: Or, reworking to one fold and a show
m (→{{header|REXX}}: changed spelling of Munchhausen to Münchhausen.) |
(→{{header|Haskell}}: Or, reworking to one fold and a show) |
||
Line 434:
main = print $ filter isMunchausen [1..5000]</lang>
{{out}}
<pre>[1,3435]</pre>
The Haskell libraries provide a lot of flexibility – we could also rework the sum, map, and unfold above to a single fold and a show:
<lang haskell>import Data.Char (digitToInt)
isMunchausen :: Int -> Bool
isMunchausen n = n == foldl plusTetra 0 (show n)
where
plusTetra n c =
let v = digitToInt c
in n + v ^ v
main :: IO ()
main = print $ filter isMunchausen [1 .. 5000]</lang>
{{Out}}
<pre>[1,3435]</pre>
|