<div dir="ltr"> I see: what matters is that ++ has to traverse to the end of the first list. The order in which its two arguments are evaluated is irrelevant.<br><br>Thank you, David and Arjun!<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Feb 6, 2015 at 5:38 PM, Arjun Comar <span dir="ltr"><<a href="mailto:nrujac@gmail.com" target="_blank">nrujac@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">Jeff,<br>An expression of the form xs ++ [x] appends the element to the end of the list. In order to evaluate the result, the ++ function iterates through the elements of the first list to find the end of the list then sets the tail of the last element of the first list to the second list. This means that on each recursive call (once per digit), your algorithm must traverse the list of digits already produced to attach the newest element. You can avoid this problem by instead consing the new digit onto the front of the list, and then reverse the list at the end to produce the digits in the correct order. Alternatively, you could try and compute the digits in the reverse order.<br><br>Here's another solution, though it also will only work on positive integers and will fail on negative inputs:<br><br>    import Data.Char (digitToInt)<br><br>    lastDigit :: Int -> [Int]<br>    lastDigit x = map digitToInt (show x)<br><br>You can fix the issues with this implementation by replacing digitToInt with a wrapper that checks if the character being analyzed is in fact a digit and then providing something if it is not. What you should provide in the failure case is up to the requirements of the problem.<br><br>Thanks,<br>Arjun<br></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Fri, Feb 6, 2015 at 8:01 PM, Jeffrey Brown <span dir="ltr"><<a href="mailto:jeffbrown.the@gmail.com" target="_blank">jeffbrown.the@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr">Is it successively appending? My intention was to prepend. If it's appending, Is that because the ++ operator always evaluates its left argument first, then its right one?<br></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Feb 6, 2015 at 2:42 PM, David Feuer <span dir="ltr"><<a href="mailto:david.feuer@gmail.com" target="_blank">david.feuer@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span><p dir="ltr"><br>
On Feb 6, 2015 3:41 PM, "Jeffrey Brown" <<a href="mailto:jeffbrown.the@gmail.com" target="_blank">jeffbrown.the@gmail.com</a>> wrote:<br>
><br>
> Here's a solution:<br>
> lastDigit x = mod x 10<br>
> remainingDigits x = (x - lastDigit x) `div` 10<br>
> listDigits x<br>
>   | x < 10 = [x]<br>
>   | otherwise = (listDigits $ remainingDigits x) ++ [lastDigit x]<br>
><br>
> Here's a faster one:<br>
> listDigits2 x<br>
>   | x < 10 = [x]<br>
>   | otherwise = (listDigits $ remainingDigits) ++ [lastDigit]<br>
>   where lastDigit = mod x 10<br>
>         remainingDigits = (x - lastDigit) `div` 10</p>
</span><p dir="ltr">These give somewhat peculiar results when the argument is negative. That can be rectified, of course. Unfortunately, all of these proposed solutions have a serious performance problem: successively appending single elements to build a list of length n is O(n^2). There's an easy fix, which I'll let you come up with.</p><span><font color="#888888">
<p dir="ltr">David</p>
</font></span></blockquote></div><br></div>
</div></div><br></div></div><span class="">_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></span></blockquote></div><br></div>
</blockquote></div><br></div>