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
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...
New
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
I’m posting this in response to Jose’s recent tweet (Cr. link) :
People are sleeping on Elixir for a coding harness:
Hot-code swappi...
New
Hello,
I wrote Stop My Hand, a Scattergories-like web application using Phoenix/LiveView as my learning project for Elixir (after readin...
New
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes.
We’re a small ...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
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
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










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.