# Recusion patterns

**URL:** https://discourse.elm-lang.org/t/recusion-patterns/4867
**Category:** Learn
**Created:** [December 22, 2019, 3:47pm UTC](https://discourse.elm-lang.org/t/recusion-patterns/4867 "2019-12-22T15:47:16Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![simonh1000](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/simonh1000/32/100_2.png) [@simonh1000](https://discourse.elm-lang.org/u/simonh1000)
#### Post date: [December 22, 2019, 3:47pm UTC](https://discourse.elm-lang.org/t/recusion-patterns/4867/1 "2019-12-22T15:47:16Z")

</div>

```auto
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.

---

<div class="post-metadata">

### Author: ![folkertdev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/folkertdev/32/133_2.png) [@folkertdev](https://discourse.elm-lang.org/u/folkertdev)
#### Post date: [December 22, 2019, 3:53pm UTC](https://discourse.elm-lang.org/t/recusion-patterns/4867/2 "2019-12-22T15:53:16Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jfmengels](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jfmengels/32/3124_2.png) [@jfmengels](https://discourse.elm-lang.org/u/jfmengels)
#### Post date: [December 22, 2019, 3:55pm UTC](https://discourse.elm-lang.org/t/recusion-patterns/4867/3 "2019-12-22T15:55:07Z")

</div>

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

---

<div class="post-metadata">

### Author: ![simonh1000](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/simonh1000/32/100_2.png) [@simonh1000](https://discourse.elm-lang.org/u/simonh1000)
#### Post date: [December 22, 2019, 4:38pm UTC](https://discourse.elm-lang.org/t/recusion-patterns/4867/4 "2019-12-22T16:38:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)
#### Post date: [January 1, 2020, 4:38pm UTC](https://discourse.elm-lang.org/t/recusion-patterns/4867/5 "2020-01-01T16:38:42Z")

</div>

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