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

Zvi <>
Thu Sep 18 12:21:19 CEST 2008

```Richard,

1. if you already dealing with it, can you also extend it to floats too?
I.e. if step is float, then the resulting list must be floats too.

-spec(seq/1 ::
(integer()) -> [integer()]).  % is. lists:seq(Max)  ==
lists:seq(1,Max) == lists:seq(1,Max,1)

-spec(seq/2 ::
(integer(),integer()) -> [integer()]).
-spec(seq/2 ::
(integer(), float()) -> [integer()]).
-spec(seq/3 ::
(integer(),integer(),integer()) -> [integer()]).

-spec(seq/2 ::
(float(),integer()) -> [float()]).

number = float() | integer()

-spec(seq/3 ::
(number(),number(),float()) -> [float()]).

2. I'm all about common sence, and behaviour we expect must be consistent
with similar constructs in other languages from Basic to Matlab.
Python's   "range([start,] stop [,step]) -> list of integers"  construct  is
not a good example, b/c "stop" isn't included (which is counter-intuitive)
and it's also defined only on integers. See example at the end of the
message.

3. Step = 0
Also Erlang handles well case when Step=0, while both Matlab and Python not:

Erlang:
---------------------------
1> lists:seq(1,1,0).
[1]

Matlab:
-----------
>> 1:0:1
ans =
Empty matrix: 1-by-0

Python:
---------------------
>>> range(1,2,0)

Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
range(1,2,0)
ValueError: range() step argument must not be zero
>>>

VBScript:
-----------
For i=1 To 1 Step 0: Print i: Next
1
1
1
1
... endless loop

3. Syntax.
Also will be good to have operator or parse-transform, which converting
something like "[N..M]"  to "lists:seq(N,M)" or "[N1,N2..M]" to
"lists:seq(N1,M,N2-N1)"

4 Is this possible to have iterator or lazy evaluated sequence in Erlang?
I.e. in code like this:

Sum = lists:sum( [X*X || X<-lists:seq(1,1000000)] ).

Do I really need to allocate list of 1000000 elements, just to be able to
use List Comprehension construct?

5 Last thing, I think that function like this must be implemented in native
code in the VM itself, like lists:reverse.

Examples:

Matlab:
------------------
>> 1:0
ans =
Empty matrix: 1-by-0

Python:
-----------------
>>> range(1,0)
[]
>>> range(1,1)
[]

VBScript:
----------
For i=1 To 0: Print i: Next
Done!

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

RESULT MUST BE:  []

Erlang:
-----------------------
1> lists:seq(0,1,0.1).
** exception error: bad argument in an arithmetic expression
in function  lists:seq/3

RESULT MUST BE:  [0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]

Matlab:
-------------
>> 0:0.1:1
ans =
0  0.1000   0.2000   0.3000   0.4000   0.5000   0.6000   0.7000
0.8000   0.9000   1.0000

VBScript:
----------
For i=0 To 1 Step 0.1: Print i: Next
0
.1
.2
.3
.4
.5
.6
.7
.8
.9
1
Done!

Best Regars,
Zvi

--
View this message in context: http://www.nabble.com/lists%3Aseq--2%2C3--bug--tp19547026p19549838.html
Sent from the Erlang Questions mailing list archive at Nabble.com.

```