[erlang-questions] lists:seq/[2,3] bug?

Richard A. O'Keefe ok@REDACTED
Thu Sep 18 08:41:28 CEST 2008


On my Mac I have Erlang R12B-3.
erl -version says
Erlang (SMP,ASYNC_THREADS) (BEAM) emulator version 5.6.3

lists:seq(1, N) gives me the expected N-element list for
integers N >= 1.  I expect, indeed, I would require, that
lists:seq(1, 0) should give me the empty list.  Instead,

2> lists:seq(1, 0).
** exception error: no function clause matching lists:seq(1,0)
3> lists:seq(1, 0, 1).
** exception error: no function clause matching lists:seq(1,0,1)

Here's a version of seq/2 that does what I expect.

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

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.

We expect length(lists:seq(L, U)) == U-L+1.
It does make sense to report an error when U < L-1,
because there is no way the result can have negative
length.  But the empty list is a perfectly good list.

If people agree that this is a bug I'll provide a version
of lists:seq/3 that behaves itself.  We do expect that
lists:seq(L, U) and lists:seq(L, U, 1) will give the
same answer, after all.

--
If stupidity were a crime, who'd 'scape hanging?










More information about the erlang-questions mailing list