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
Trending in Discussions
The obligatory hello world thread!
Who are you and where are you from? :stuck_out_tongue:
New
Hey there,
It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
I’ve been tinkering with Lynx (by José) via hold_ledger. I used AI to write the code, a separate specification, the tests, and the Lean p...
New
Hi everyone! I’m the developer of PaperForge, and I’ve been working on something alongside it that I’d like your opinion on.
I’m conside...
New
A remark @Garrison made…
..has already inspired a thread for the technical side of reclaiming our internet, but there is of course anot...
New
This is where the projects in the Hologram Showcase get posted: apps built with Hologram and libraries made for it. New ones go up usuall...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #security
- #processes
- #api
- #forms
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Dusty
One issue that jumps out right away is that you are not binding the expression:
to any variable (in order to capture the new value).
kokolegorille
You might simplify with
We live in an immutable world
final_list is not changing…
srowley
You are getting the error because
tailis a list, but you are passing it tosplit/2which expects the first argument to be a string.split/2takestailand tries to callString.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:
owaisqayum
You are right, but here am just appending the head to final list right or am doing something wrong ?
owaisqayum
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
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
Is it because we have fixed arguments or is there any other reason as well?
Also, in this case
it’s concatenating a string with a list, how it actually works?
srowley
String.split/2returns 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/1is 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
Thank you for such a detailed response.
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
In this case, the last expression in your function is
which calls
split/2using the same value forfinal_listthat was passed to the function originally. You have already discussed thattailis causing a type error in this case, but regardless, the result of your list concatenation operation is simply thrown away.