functional programming model efficiency (Erlang specific)
erlang, functional-programming
Solution
First of all, read the Erlang efficiency guide on recursions.
As for the clumsiness, don't forget that Erlang lists are single-linked lists, so you only have a "pointer" to the head of the list, and need to access elements by traversing the list. This would require the same amount, but different kind of clumsiness from those languages with all the pointer or reference juggling.
As for efficiency, you can implement it in a tail recursive fashion. Tail recursion is optimized (see this SO question) in a way that the compiled code becomes similar to what you implement in C++, only difference is that instead of the code pointer jumping around, the stack pointer is rewind, etc.
Anyway, try to implement the very same functionality in Java and C++ and then we will see which one is clumsier and more readable.
Problem
Hi I am a newbie in the Erlang world. When I think of how we need to solve the following problem (and there are a long list of similar ones), I think it's really inefficient because we are speaking of a lot of recursion. Apprently, language like C/Java would not need the clumsy recursion to solve this problem, but with Erlang (I guess other functional programming language needs to as well, maybe?) you must do in such a way. Example 3 - Append This program concatenates two lists: ``` append([], List) -> List; append([First|Rest], List) -> [First | append(Rest,List)]. ``` Can anyone give an explanation why this is not a problem ?