[erlang-questions] Question about reverse list of recursion functions

Dmitry Kolesnikov dmkolesnikov@REDACTED
Sun Dec 27 11:12:58 CET 2015


Hello,

The reverse is acceptable if you are using lists:reverse/1 (this is built-in function written in C).
 
There is an alternative, you can use lists:foldr function to avoid unnecessary reverse operation:

```

divide_to_lesser_and_bigger(List, X) ->
   lists:foldr(fun(E, Acc) -> lesser_or_bigger(E, X, Acc) end, {[], []}, List).

lesser_and_bigger(E, X, {Lesser, Bigger}) when E < X ->
   {[E|Lesser], Bigger};
lesser_and_bigger(E, _, {Lesser, Bigger}) ->
   {Lesser, [E|Bigger]}.

```
  
- Dmitry



> On Dec 27, 2015, at 5:38 AM, Andrey Koleshko <ka8725@REDACTED> wrote:
> 
> Hi, guys! I recently started to learn Erlang (migrating from Ruby) by reading “Erlang Programming” Cesarini book. After the 3rd chapter there is a task to implement quick sort and I implemented it like this: https://gist.github.com/ka8725/f3fcc264e12bcefa6035
> It works well, but I have a question that doesn’t allow to sleep me - is it normal practice to do `reverse` every time when you have an list with opposed direction after a few recursions calls? Is there are other practice to avoid it? Thanks!
> ________________
> Best Regards,
> Andrey Koleshko
> ka8725@REDACTED
> 
> 
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list