Closed jmbromley closed 3 months ago
This is fine as far as it goes. I wasn't there for the original issue, but is there a reason why the function wasn't made tail recursive to begin with?
As the original author: no, there's no specific reason why I didn't write them in a tail-recursive way. Chalk that up to inexperience :) I'd prefer a tailrec version over a fast version.
I actually made a PR that introduced separate tail recursive versions of the functions here: elm-community/list-extra#165
At the time @Chadtech thought it was too much of a corner case to risk a decrease in efficiency (https://github.com/elm-community/list-extra/issues/164#issuecomment-1140339068)
But now that I search the repository more carefully, I see that a few months prior to my PR there had been an effort to make it tail recursive (elm-community/list-extra#157) which was in fact merged, but did not actually fix the issue (see below). In that previous PR the discussion seemed to indicate that it was felt by many that correctness was preferred over efficiency, so maybe I should have pushed for it harder at the time.
The reason the previous PR didn't actually make the functions tail recursive is because it still uses List.take
from elm/core which only switches to a tail recursive implementation for lists larger than 1000 elements. This is a problem because the implementation of the groupsOf...
family of functions is itself defined by recursion, so you can end up with the non-tail-recursive List.take
being called a large number of times and blowing the stack (a real world example is at billstclair/elm-crypto-string#10).
If you want to use a fully tail recursive version in all cases, it should be fairly straightforward to adapt my PR to do so - I'm happy to make a new PR if you'd like?
Okay, so I made a pull request that instead makes groupsOf...
fully tail recursive (#47) as an alternative to this PR.
Closed by #47
Add warning message for
groupsOf...
functions in the documentation inList.Extra
as suggested in https://github.com/elmcraft/core-extra/issues/44#issuecomment-1968594245