questions about lists module implementations.

Angel Alvarez clist@REDACTED
Thu Dec 10 23:37:03 CET 2009


Hi

Im reading otp sources trying to check my skills as i progress onto the language.

Two function got my attention:


%% reverse(L) reverse all elements in the list L. Is now a BIF!

-spec reverse([T]) -> [T].

reverse([] = L) ->
    L;
reverse([_] = L) ->
    L;
reverse([A, B]) ->
    [B, A];
reverse([A, B | L]) ->
    lists:reverse(L, [B, A]).

%reverse([H|T], Y) ->
%    reverse(T, [H|Y]);
%reverse([], X) -> X.

Well, in fact is not clear for me why current function is a BIF (do BIF functions have precedence over erlang coded ones?)
cause being a BIF i dont understand why is still on the lists module source...

Is there a risk of calling the wrong function if you make a function named like a BIF?

i think of mytest:reverse vs reverse (BIF).


why is better than the previous incarnation (commented out).?

The second issue comes from the strange way seq is defined. 
Is that a form of loop unrolling?


-spec seq(integer(), integer()) -> [integer()].

seq(First, Last)
    when is_integer(First), is_integer(Last), First-1 =< Last ->
    seq_loop(Last-First+1, Last, []).

seq_loop(N, X, L) when N >= 4 ->
     seq_loop(N-4, X-4, [X-3,X-2,X-1,X|L]);
seq_loop(N, X, L) when N >= 2 ->
     seq_loop(N-2, X-2, [X-1,X|L]);
seq_loop(1, X, L) ->
     [X|L];
seq_loop(0, _, L) ->
     L.


/Angel
-- 
Agua para todo? No, Agua para Todos.
->>-----------------------------------------------
    Clist UAH a.k.a Angel
---------------------------------[www.uah.es]-<<--



More information about the erlang-questions mailing list