New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List.take throws "Maximum call stack size exceeded" with large numbers #601

Closed
tgecho opened this Issue May 13, 2016 · 2 comments

Comments

Projects
None yet
2 participants
@tgecho

tgecho commented May 13, 2016

This happens if you try to take with an n greater than about 5707 and the list you're taking from has at least that many items. This did not happen in v0.16.

In elm-repl:

> List.take 5707 [0..10000]
RangeError: Maximum call stack size exceeded
@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender May 28, 2016

Contributor

This version would not have that stack explosion issue:

take : Int -> List a -> List a
take n list =
  List.reverse (takeHelper n list [])

takeHelper : Int -> List a -> List a -> List a
takeHelper n list acc =
  if n <= 0 then
    acc

  else
    case list of
      [] ->
        acc

      x :: xs ->
        takeHelper (n - 1) xs (x :: acc)
Contributor

jvoigtlaender commented May 28, 2016

This version would not have that stack explosion issue:

take : Int -> List a -> List a
take n list =
  List.reverse (takeHelper n list [])

takeHelper : Int -> List a -> List a -> List a
takeHelper n list acc =
  if n <= 0 then
    acc

  else
    case list of
      [] ->
        acc

      x :: xs ->
        takeHelper (n - 1) xs (x :: acc)
@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
Contributor

jvoigtlaender commented Jul 18, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment