<div dir="ltr">Thanks, is the notation with the single quote (isKeith') a convention for helper functions or does it actually change something about the program?<div>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.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Nov 10, 2014 at 8:50 PM, Rein Henrichs <span dir="ltr"><<a href="mailto:rein.henrichs@gmail.com" target="_blank">rein.henrichs@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">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.<div><br></div><div>(Also please consider the criterion library for benchmarking.)<br><div><div><br></div><div><div>> import Data.Digits</div><div><br></div><div>> isKeith :: Int -> Bool</div><div>> isKeith n = let ds = digitsRev 10 n in isKeith' n (length ds) ds</div><div><br></div><div>> isKeith' :: Int -> Int -> [Int] ->  Bool</div><div>> isKeith' n len ds</div><span class=""><div>>   | n == s = True</div><div>>   | s > n = False</div></span><div>>   | otherwise = isKeith' n len (take len (s : ds))</div><div>>   where s = sum ds</div><div><br></div><div>> main :: IO ()</div><div>> main = print (isKeith 197)</div></div><div><br></div><div>True</div><div><br></div></div></div></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>