Recusion patterns

runCodeInner : ProgState -> ProgState
runCodeInner step =
    case step of
        Running state ->
            let
                tmp =
                    doStep state
            in
            runCodeInner tmp

        --Running state ->
        --    state
        --        |> doStep
        --        |> runCodeInner
        --
        _ ->
            step

This snippet is from my Advent of Code work. My original code was the commented out part and I was getting a stack overflow. I had reached a question where there were warnings that things could take a while on slow computers, so I had a feeling that the issue was my recursion pattern.

I’m not an expert in ‘tail recursion’ but I thought the original code was tail recursive? It seems not, but that my alternative is and completed quickly. Something to do with evaluating it to tmp then I guess?

There’s clearly something I can learn here about how recursion works in Elm and would welcome feedback on that.

This is a change with the desugaring of |> in 0.19. Previously, this happened early on, now it happens after tail-recursion is recognized.

Tail-recursive statements now have to look like f x y z. The seemingly equivalent z |> f x y is not recognized as tail-recursive.

3 Likes

Just like @folkertdev said. An issue was raised in the compiler, which you can read here: https://github.com/elm/compiler/issues/1770

2 Likes

Yikes - this feels like a real gotcha at the heart of Elm. Thanks for pointing me to the issue

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.