Skip to content
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

Cannot use tail-end recursion because of laziness #2

Open
aaronshim opened this issue Jun 3, 2017 · 0 comments
Open

Cannot use tail-end recursion because of laziness #2

aaronshim opened this issue Jun 3, 2017 · 0 comments

Comments

@aaronshim
Copy link

aaronshim commented Jun 3, 2017

Hello,
After debugging a runtime error (shocking!) in my application, I found that guards may cause stack overflows when used in conjunction with a recursive call due to the non-lazy nature of Elm. (Coming from the Haskell world, this was totally shocking and completely unexpected for me!)

To provide a simple repro case, here is a snippet using elm-guards

foo : Int -> Int
foo x = x <= 0 => x
     |= foo (x - 1)

but this will stack overflow because the foo (x - 1) is attempted to be evaluated even when the conditions are met to return from the first branch. In comparison, the following code, using if (...) then (...) else (...) constructs instead, does not try evaluating branches it does not need:

foo_ : Int -> Int
foo_ x =
    if x <= 0 then
        x
    else
        foo_ (x - 1)

and foo_ 5 will evaluate to 0 as expected.

Are there plans to make this library usable with recursion? If not, can we at least get a warning in the README?

Thank you for the library and for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant