-- core function; read from bottom to topf::Int->Intf=sum-- Sum up the squares.map((^2)-- Compute squares.read-- Convert digit strings to Ints.(:[]))-- Explode number string to list of digit strings.show-- Convert input number to string-- This is a folding pattern with a tricky break condition,-- depending on the list element and the accumulator.foldP::(a->b->b)->(a->b->Bool)->b->[a]->bfoldPfpacc[]=accfoldPfpacc(x:xs)|pxacc=acc|otherwise=foldPfp(fxacc)xs-- Cut off a list's tail, once a number is repeating.-- (reverts the preserved part of the list)cut::Eqa=>[a]->[a]cut=foldP(:)elem[]-- fold by appending and checking if x is already in list-- Iterate the core function, cut the result list and check if the final-- result is 1.isHappy::Int->BoolisHappy=(==1).head.cut.iteratefmain=print$filterisHappy[1..200]

Note that Haskell function composition with the (.) operator is read from right to left (or from bottom to top, respectively). The hardest part for FP is folding with a non-trivial break condition, here.

Here is my solution, to keep me fresh in haskell (I rarely have the possibility to use it in projects and thus never learned it properly):

-- Square summationsquareSum::[Integer]->IntegersquareSum=sum.map(^2)-- Splitting n into its digitssplitNum::Integer->[Integer]splitNumn|n<10=[n]|otherwise=(splitNum$quotn10)++[n`mod`10]-- Check for happinessisHappy::Integer->BoolisHappy1=TrueisHappy4=False-- 4 is in every unhappy cycleisHappyn=(isHappy.squareSum.splitNum)nmain=print$filterisHappy[1..200]

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.

importControl.Monad.StateimportControl.Monad.Loops(untilM)swap::(a,b)->(b,a)swap(x,y)=(y,x)-- digit parserdigit::Integrala=>Stateaadigit=state$\s->swap$quotRems10-- eof conditioneof::Integrala=>StateaBooleof=get>>=\s->return$s==0-- parsing the digits of a number into a (reversed) list of digitsdigits::Integrala=>a->[a]digits=evalState(digit`untilM`eof)-- core functionf=sum.map(^2).digits-- iteration of f by recursion of isHappy isHappyn|n==4=False|n==1=True|otherwise=isHappy(fn)

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. :)

And now that I have learned a simpler break condition for the problem, it is easier:

f::Int->Intf=sum-- Sum up the squares.map((^2)-- Compute squares.read-- Convert digits to Ints.(:[]))-- Explode number string to list of digit strings.show-- Convert input number to stringisHappy::Int->BoolisHappyn|n==4=False|n==1=True|otherwise=isHappy(fn)main=print$filterisHappy[1..200]

On the other hand, I like the possibility of my original code to see the list of intermediate results to get a feeling for the problem.

## re: Write a script to find "Happy Numbers" VIEW POST

FULL DISCUSSIONAdding a solution in Haskell:

Note that Haskell function composition with the (.) operator is read from right to left (or from bottom to top, respectively). The hardest part for FP is folding with a non-trivial break condition, here.

Here is my solution, to keep me fresh in haskell (I rarely have the possibility to use it in projects and thus never learned it properly):

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.

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. :)

And now that I have learned a simpler break condition for the problem, it is easier:

On the other hand, I like the possibility of my original code to see the list of intermediate results to get a feeling for the problem.