[erlang-questions] lists:seq/3 strange behaviour

Pascal Chapier <>
Tue Jun 8 17:53:30 CEST 2010

OOps, I answer too fast,

I think the the code should be

 Last, Inc) 
    when is_integer(First), is_integer(Last), 
is_integer(Inc) -> 
        Inc > 0, First =< 
        Inc < 0, First >= Last ->
            N = 
(Last - First + Inc) div Inc,
Inc*(N-1)+First, Inc, []);
        Inc =:= 0, First =:= Last ->
 seq_loop(1, First, Inc, [])

In this case we get:
 an exeption for both for lists:seq(2, 1, 10) and lists:seq(20, 1, 10).
 a list of one element if inc > (last-first) positive case

least the behavior is symetric for positive and negative increment.

 know, the "let it crash" is more in conformance with the Erlang usage 
As the good behavior depends on the the application use case, may
 be you are right, and the application will have to include a try/catch 
if the case is likely to appear.


> Date:
 Tue, 8 Jun 2010 15:58:07 +0200
> From: 

> To: 
> Subject: Re: [erlang-questions]
 lists:seq/3 strange behaviour
> Piotr wrote:
> What was the reason for lists:seq function to be implemented in a 
way that
> if you call:
> > lists:seq(2, 1, 10)
> it works as expected returning an an empty list, while when beeing 
> like:
> > lists:seq(20, 1, 10)
> > it 
throws an exception?
> Pascal wrote:
> > Or, a 
solution that I prefer, add a default condition which return an
empty list as you suggest.
> In earlier versions (for 
example R12B-5) lists:seq/3 on the examples above
> throws a 
function-clause exception which makes more sense than a missing
true-branch in an if-clause. I would prefer stronger guards and a
 function-clause exception over a solution which returns an empty list 
> because the function is undefined for those cases.
> /Jesper Pettersson
La boîte mail NOW Génération vous permet de réunir toutes vos boîtes mail dans Hotmail !

More information about the erlang-questions mailing list