# Unexpected results with lists:reverse/1

Hi all,
I’ve just started programming Erlang, to evaluate if is useful for my company.
But…I’ve immediately got a (stupid?) problem. Here’s the code:

``````-module(hello).

-export([range/2]).

private_range(1, F) ->
[F(1)];
private_range(N, F) ->
[F(N) | range(N - 1, F)].

range(N, F) ->
lists:reverse(private_range(N, F)).
``````

why the result is

``````36> hello:range(8, fun(N) -> N end).
[7,5,3,1,2,4,6,8]
``````

``````[1,2,3,4,5,6,7,8]
``````

thanks,
regards,
G.

3 Likes

Maybe you mean

``````private_range(1, F) ->
[F(1)];
private_range(N, F) ->
[F(N) | private_range(N - 1, F)].
``````

?

4 Likes

ops yeah sorry

2 Likes

That outcome is kinda cool, though

6 Likes
``````[1 | [2 | [3]]] = [1, 2, 3]
``````

you are already putting the stuff ordered but without using tail-recursive.

``````private_range(N, F) ->
private_range(N, F, []).

private_range(0, _F, Acc) ->
lists:reverse(Acc);
private_range(N, F, Acc) ->
private_range(N - 1, F, [F(N) | Acc]).
``````
1 Like