[Haskell-beginners] Finding Keith Numbers with Haskell

Jack Mott jack.mott at gmail.com
Tue Nov 11 13:26:53 UTC 2014


Thanks, is the notation with the single quote (isKeith') a convention for
helper functions or does it actually change something about the program?
I did try this approach as well, reversing the digits and adding elements
to the head, however having to use take and letting the list build  up
larger seems to result in slightly worse performance overall.

On Mon, Nov 10, 2014 at 8:50 PM, Rein Henrichs <rein.henrichs at gmail.com>
wrote:

> Since summing is commutative (sum [a,b] = sum [b,a]), it doesn't matter
> what order they appear in. You can start out by reversing the digits and
> then by taking from and adding to the beginning of the list instead of the
> end. Also, isKeith needs to know the length of the decimal representation
> of n. Since you are already computing that representation in isKeithHelper,
> it might be best to compute this there as well and pass it into isKeith.
>
> (Also please consider the criterion library for benchmarking.)
>
> > import Data.Digits
>
> > isKeith :: Int -> Bool
> > isKeith n = let ds = digitsRev 10 n in isKeith' n (length ds) ds
>
> > isKeith' :: Int -> Int -> [Int] ->  Bool
> > isKeith' n len ds
> >   | n == s = True
> >   | s > n = False
> >   | otherwise = isKeith' n len (take len (s : ds))
> >   where s = sum ds
>
> > main :: IO ()
> > main = print (isKeith 197)
>
> True
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20141111/0fabea7b/attachment.html>


More information about the Beginners mailing list