[erlang-questions] questions about lists module implementations.
Richard O'Keefe
ok@REDACTED
Fri Dec 11 03:06:03 CET 2009
On Dec 11, 2009, at 11:37 AM, Angel Alvarez wrote:
>
> 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.
It is precisely an unrolled loop.
Not something you want to do in user code,
but justifiable in library code if measurements show it pays off.
(If you want to see library code chock full of stuff no sane
programmer wants to write, but quite justifiably so, take a
look at the LAPACK sources some day.)
More information about the erlang-questions
mailing list