Munchausen numbers: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Further reduced the digitToInt version)
Line 794: Line 794:
The Haskell libraries provide a lot of flexibility – we could also rework the sum, map, and unfold above to a single fold:
The Haskell libraries provide a lot of flexibility – we could also rework the sum, map, and unfold above to a single fold:


<lang haskell>import Data.Char (digitToInt)
<lang haskell>import Control.Monad (join)
import Data.Char (digitToInt)


isMunchausen :: Int -> Bool
isMunchausen :: Int -> Bool
isMunchausen n =
isMunchausen =
let go = (+) . join (^) . digitToInt
n ==
foldr
in (==) <*> foldr go 0 . show
(\c n ->
let v = digitToInt c
in n + v ^ v)
0
(show n)


main :: IO ()
main :: IO ()
main = print $ filter isMunchausen [1 .. 5000]</lang>
main = print $ filter isMunchausen [1 .. 5000]</lang>

{{Out}}
{{Out}}
<pre>[1,3435]</pre>
<pre>[1,3435]</pre>