przemyxe0p
What do you do if you’ve done an exercise, it works, it’s not very inefficient and seems concise, then you go to the community solutions, sort by highest-rated user and see this?
defmodule Sublist do
@doc """
Returns whether the first list is a sublist or a superlist of the second list
and if not whether it is equal or unequal to the second list.
"""
def compare(a, b) when is_list(a) and is_list(b) do
case {contains?(a, b), contains?(b,a)} do
{true, true} -> :equal
{true, false} -> :superlist
{false, true} -> :sublist
{false, false} -> :unequal
end
end
# determines if list a contains list b. restore_a is needed to restore
# already "eaten" members of a, when b couldn't be matched completely
defp contains?(a, b, current_b \\ :initial, restore_a \\ nil)
defp contains?(a, b, :initial, nil), do: contains?(a, b, b, nil)
defp contains?(_, _, [], _), do: true
defp contains?([], _, _, _), do: false
defp contains?([x | a], b, [x | c], nil), do: contains?(a, b, c, a)
defp contains?([x | a], b, [x | c], restore_a), do: contains?(a, b, c, restore_a)
defp contains?([_ | a], b, _, nil), do: contains?(a, b, b, nil)
defp contains?(_, b, _, restore_a), do: contains?(restore_a, b, b, nil)
end
To clarify I enjoy creating solutions, and having to think, and play, but there is border.
Trending in Challenges
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 am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
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
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
mudasobwa
Create your own wrapper
my_contains?/4which would print the intermediate results and run it for several different inputs.derek-zhou
The code you cited seems fine to me, pretty clever use of tail recursion. I am not sure what do you mean. do you mean:
przemyxe0p
It’s just too hard for me. I got overwhelmed
So, I’ve been getting better at reading community solutions, but still I don’t know in what order to read function clauses to understand it. Now sometimes i get it right away how sth works like i jump through lines with correct order to understand it fast. But How should you approach reading this kind of multiclause functions?
derek-zhou
The correct reading order is from the top to bottom. If you want to improve your skill on tail recursion, write with it. Can you implement the enumerating functions in
Enum, starting withEnum.reverse/1, with tail recursion? If you can write comfortably in tail recursion, it will click for you.przemyxe0p
Yes i did it many times, implementing Enum.reverse/1 is easy for me.
for example right now i wrote function to flatten list:
The problem is 5 minutes later when i look at the code i want to add something, e.x ommiting some values, i cannot comprehand what is happening
XD
przemyxe0p
Reading from top to bottom is not always the way (for me) to understand how function will behave.
derek-zhou
nitpick, can’t help it.
przemyxe0p
I guess we should reverse at the end, not on intermediate states, and your suggestion would not work (I tested it).
How to tell if my above solution is proper tail-call recursion?