greenapple
Recursive concatenation
Hi,
I’m a beginner, and I’m trying to solve a rather simple question but I’m kinda stuck. I’m trying to write a fully-recursive function that appends a list to the other one, and I’m also not allowed to use any pre-written functions (like Enum.concat or ++ ). I’m honestly not even sure where I would start. I imagine there would be some pattern matching involved and maybe a base case with just one element in each list but I’m not sure. Can somebody help me with this please?
Marked As Solved
benwilson512
Right perfect. And if you wanted to do a concat on a list that just took one item, you could do:
def concat([item], list) do
[item | list]
end
concat([1], [2,3] #=> [1,2,3]
The trick then is to just apply this logic recursively. When doing recursive logic, it’s always a good idea to identify the “base case” which you can think of as “when do I stop recursing?”. In the case of this kind of concat function, you stop recursing when you have just 1 item in the list, because at that point you do a simple prepend.
When you have more than one item, you need to recursively prepend until you hit the base case:
def concat([], list), do: list
def concat([item], list) do
[item | list]
end
def concat([item | rest], list) do
[item | concat(rest, list)]
end
The first part of [item | concat(rest, list)] is [item | which you’re already familiar with, that’s setting up item to be prepended to whatever is on the right hand side of |. But this time, instead of prepending it to | list] we do | concat(rest, list)] because we want the item to come before the result of prepending all of the other items.
Also Liked
greenapple
I think I understand the solution. Thank you so much for the great explanation, it is very much appreciated!
benwilson512
Great!
So let’s start with something very basic: How would you do this simpler function:
prepend(1, [2,3]) #=> [1,2,3]
NobbZ
I think you are missing an edge case here: concat([], [1,2,3])
benwilson512
Hi @greenapple, welcome!
Just to make sure I understand what you’re going for, you’re trying to achieve basically:
concat([1,2], [2,4]) #=> [1,2,3,4]
greenapple
I would use something like
[1 | [2, 3]]







