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

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…

Last Post!

Dusty

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.

Where Next?

Popular in Discussions Top

und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
pdgonzalez872
If this has been asked here before, please point me to where it was asked as I didn’t find it when I searched the forum. Maybe a mailing ...
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement