owaisqayum

owaisqayum

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

Showing Posts 1 to 10

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…

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
owaisqayum

owaisqayum OP

You are right, but here am just appending the head to final list right or am doing something wrong ?

owaisqayum

owaisqayum OP

Wont it just return the value as we that in elixir the last expression of a function gets returned automatically. I might be wrong, can you kindly advice ?

owaisqayum

owaisqayum OP

you are absolutely right and it’s such a dumb mistake to make. A very solid way of using pattern matching. One question, why you have used a tuple and why not a list

    {first_three_characters, rest_of_word} = String.split_at(string, 3)

Is it because we have fixed arguments or is there any other reason as well?

Also, in this case

[first_three_characters | final_list]

it’s concatenating a string with a list, how it actually works?

srowley

srowley

String.split/2 returns a tuple, so only a tuple will match on the result returned by that function.

I am prepending the string to an accumulated list of strings. This is a common practice as prepending to a list is more efficient than appending to one. That is also why Enum.reverse/1 is called at the end, because prepending ends up generating a list of items in reverse that needs to be reversed again to preserve the original order.

owaisqayum

owaisqayum OP

Thank you for such a detailed response.

mpope

mpope

I think reading this section in the docs on the ‘left hand copy’ of the ++ operator could clarify why prepending is faster, for future reference.

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.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews