[erlang-questions] lists:seq/3 strange behaviour
Pascal Chapier
pascalchapier@REDACTED
Tue Jun 8 17:53:30 CEST 2010
OOps, I answer too fast,
I think the the code should be
seq(First,
Last, Inc)
when is_integer(First), is_integer(Last),
is_integer(Inc) ->
if
Inc > 0, First =<
Last;
Inc < 0, First >= Last ->
N =
(Last - First + Inc) div Inc,
seq_loop(N,
Inc*(N-1)+First, Inc, []);
Inc =:= 0, First =:= Last ->
seq_loop(1, First, Inc, [])
end.
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
at
least the behavior is symetric for positive and negative increment.
I
know, the "let it crash" is more in conformance with the Erlang usage
:o)
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.
Pascal
> Date:
Tue, 8 Jun 2010 15:58:07 +0200
> From:
jesper.pettersson@REDACTED
> To: pascalchapier@REDACTED
>
CC: erlang-questions@REDACTED
> 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
called
> 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
simply
> 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 !
http://www.windowslive.fr/hotmail/nowgeneration/
More information about the erlang-questions
mailing list