owaisqayum
Recursion Issue
Hi,
I am attempting to solve a problem in Exercism “Protein Translation” and implemented the following code. I want the split(tail, final_list) to recursively add the 3 char to final_list. What am I doing wrong and how can I improve the code.
defmodule PT do
def rna(string) do
split(string, [])
end
def split(string, final_list) when string == "", do: final_list
def split(string, final_list) do
splitting = String.split_at(string, 3)
[head | tail] = Tuple.to_list(splitting)
final_list ++ [head]
split(tail, final_list)
end
end
i am getting this error:
iex(21)> PT.rna("Owaissdui")
** (FunctionClauseError) no function clause matching in String.Unicode.next_grapheme_size/1
The following arguments were given to String.Unicode.next_grapheme_size/1:
# 1
["issdui"]
Attempted function clauses (showing 10 out of 19263):
def next_grapheme_size(<<13::integer(), 10::integer(), rest::binary()>>)
def next_grapheme_size(<<"\r"::binary(), rest::binary()>>)
def next_grapheme_size(<<"\n"::binary(), rest::binary()>>)
def next_grapheme_size(<<""::binary(), rest::binary()>>)
def next_grapheme_size(<<""::binary(), rest::binary()>>)
def next_grapheme_size(<<""::binary(), rest::binary()>>)
def next_grapheme_size(<<""::binary(), rest::binary()>>)
def next_grapheme_size(<<""::binary(), rest::binary()>>)
def next_grapheme_size(<<""::binary(), rest::binary()>>)
def next_grapheme_size(<<""::binary(), rest::binary()>>)
...
(19253 clauses not shown)
Thanks
Most Liked
srowley
You are getting the error because tail is a list, but you are passing it to split/2 which expects the first argument to be a string. split/2 takes tail and tries to call String.split/2, which then throws an error because it expects its first argument to be a string, not a list.
If you simply want to split a string into lists of three characters, I can think of ways that are simpler, but using this approach I would suggest:
def split("", final_list), do: Enum.reverse(final_list)
def split(string, final_list) do
{first_three_characters, rest_of_word} = String.split_at(string, 3)
split(rest_of_word, [first_three_characters | final_list])
end
Dusty
One issue that jumps out right away is that you are not binding the expression:
final_list ++ [head]
to any variable (in order to capture the new value).
kokolegorille
You might simplify with
def split("", final_list), do: final_list
We live in an immutable world ![]()
final_list is not changing…
Last Post!
Dusty
In this case, the last expression in your function is
split(tail, final_list)
which calls split/2 using the same value for final_list that was passed to the function originally. You have already discussed that tail is causing a type error in this case, but regardless, the result of your list concatenation operation is simply thrown away.
Popular in Discussions
Other popular topics
Chat & Discussions>Discussions
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









