owaisqayum

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 Responses

srowley

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

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

kokolegorille

You might simplify with

def split("", final_list), do: final_list

We live in an immutable world :slight_smile:

final_list is not changing…

Where Next?

Popular in Discussions Top

Donovan
Hello everyone, I’m so glad to have discovered this awesome community. Thanks for creating it! This is my second post, and apologies for...
New
jeramyRR
This is an interesting article to read. Elixir’s performance, like usual, is excellent. However, it seems like the high CPU usage is co...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
WolfDan
After doing a port from a c++ library to my project in phoenix I’ve seen that I need a faster way to run this algorithm and I found this ...
New
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -&gt; H; nth(N, [_|T]) when N &gt; 1 -&gt; nth(N - 1, T). Elixir Enum.at … coo...
New
crabonature
I’m still quite new to Elixir. As I understand we got in Elixir “multi guards” as convention to simplify one large guard with or’s?: de...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
Markusxmr
Since Drab has been developed for a while in the open, introducing the Liveview functionality in a way it happend appears to undermine th...
New

Other popular topics Top

msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement