A big chunk of highly rated community solutions to DNA Encoding doesn't even use the concept that is taught, tail call recursion. Instead they use regular recursion. See at the end of the issue for examples.
It would be great if we found a way to detect that both encode and decode functions use tail call recursion. We would need to find all functions called from either function encode and decode, and check their definitions. At least one of the called functions has a function clause whose last call is itself, it's tail-call recursion.
Example 1
defmodule DNA do
@nucleotide_encode_table %{
?\s => 0b0000,
?\A => 0b0001,
?\C => 0b0010,
?\G => 0b0100,
?\T => 0b1000
}
@nucleotide_decode_table Enum.map(@nucleotide_encode_table, fn {k, v} -> {v, k} end)
|> Map.new()
def encode_nucleotide(code_point) do
Map.fetch!(@nucleotide_encode_table, code_point)
end
def decode_nucleotide(encoded_code) do
Map.fetch!(@nucleotide_decode_table, encoded_code)
end
def encode(dna)
def encode([]), do: <<>>
def encode([head | tail]) do
<<encode_nucleotide(head)::4, encode(tail)::bitstring>>
end
def decode(dna)
def decode(<<>>), do: []
def decode(<<head::4, tail::bitstring>>) do
[decode_nucleotide(head) | decode(tail)]
end
end
A big chunk of highly rated community solutions to DNA Encoding doesn't even use the concept that is taught, tail call recursion. Instead they use regular recursion. See at the end of the issue for examples.
It would be great if we found a way to detect that both
encode
anddecode
functions use tail call recursion. We would need to find all functions called from either functionencode
anddecode
, and check their definitions. At least one of the called functions has a function clause whose last call is itself, it's tail-call recursion.Example 1
Example 2
Example 2
Example 3