DEV Community

Discussion on: Write a script to find "Happy Numbers"

Collapse
 
heikodudzus profile image
Heiko Dudzus • Edited

I'm glad to see some Haskell here. :) I somehow prefer your computational approach to split the number over my quick string hack. You can easily change the base.

I have done a lot of parsing stuff in Haskell with parsec, attoparsec and readP, lately. So I instantly think of parsing when I see the problem of splitting a number into digits. The detour via strings is comfortable, because it uses the read parser. but

"Challenge" accepted to solve it computationally. :)

To import as few / basic libraries as reasonably achievable, I decided not to use the parsec library but to write a homemade digit parser.

What you do in splitNum reminds me of the quotRem function (that I found when I coded for the Challenge about Kaprekar numbers, here). quotRem just needs a swap to make for a nice digit parser in a very organic way: When dividing by 10 the integer quotient is the state of the state monad (that's still to parse) and the remainder is a parsed digit.

import Control.Monad.State
import Control.Monad.Loops(untilM)

swap :: (a,b) -> (b,a)
swap (x,y) = (y,x)

-- digit parser
digit :: Integral a => State a a
digit = state $ \s -> swap $ quotRem s 10

-- eof condition
eof :: Integral a => State a Bool
eof = get >>= \s -> return $ s == 0

-- parsing the digits of a number into a (reversed) list of digits
digits :: Integral a => a -> [a]
digits = evalState (digit `untilM` eof)

-- core function
f = sum . map (^2) . digits

-- iteration of f by recursion of isHappy 
isHappy n | n == 4 = False
          | n == 1 = True
          | otherwise = isHappy (f n)

I am a bit biased to use (faster) Ints in my prototypes, and use Integers where really needed, but I adopt your approach with Integers by at least generalizing the code to Integral types.

Although I learned to prefer folds over homemade recursion, I'm afraid the abstractness of foldP is a little bit hard to read, so perhaps I stick with the homemade recursion, this time. :)