exercism / exercism/elixir-analyzer

`dna-encoding` doesn't detect tail-recursion within a `case` expression

Offen
#410 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Elixir
Sterne
33
Forks
33
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

Based on my understanding, it seems that using a `case` expression for pattern matching, rather than a function head, prevents the automated analyzer from detecting tail recursion.

From what I've read, using a single function head with at top-level `case` expression for pattern matching should result in essentially the same thing as using multiple function heads, once they are compiled. If that's correct, then it seems the analyzer should be able to detect tail recursion in the `case` expression variant shown (commented out) below.

It's also possible that there's a gap in my understanding and having the recursive call "wrapped" in a `case` expression results in a solution that isn't tail-recursive. 😄

```elixir
def encode(dna) do
_encode(dna, <<>>)
end

# The analyzer says this function IS NOT tail recursive
# defp _encode(dna, acc) do
# case dna do
# [] -> acc
# [c | rest] -> _encode(rest, <>)
# end
# end

# The analyzer says this function IS tail recursive
defp _encode([], acc), do: acc
defp _encode([c | rest], acc), do: _encode(rest, <>)
```

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.