Haskell report (1.2) says on pg. 15,
The form [e1,e2 .. e3] denotes an airthmetic sequence from e1
in increments of e2 - e1 of values not greater than e3 (if
increment is non- -ve) or not less than e3 (if increment is
-ve). thus, the resulting list is empty if the incr. is
nonnegative and e3 < e1 or incr. is -ve and e3 > e1.
(Those who donot have Haskell report (1.2) ,the discussion is on
arithmetic sequences and to the best of my knowledge it is same to the
one found in earlier versions of the report)
But, the definitions given in the prelude doesnot match this.
Prelude PreludeCore has the foll. definitions for
enumFromThenTo n n' m ,
enumFromThenTo n n' m = takeWhile
(if n' >= n then (<= m) else (>= m))
(enumFromThen n n')
Prelude PreludeList has the following definitions,
takeWhile p [] = []
takeWhile p (x:xs)
| p x = x : takeWhile p xs
| otherwise = []
Also, [e1,e2 .. e3] = enumFromThenTo e1 e2 e3.
The above mentioned definition for enumFromThenTo does
not check whether e3 > e1 if (e2-e1) >= 0 or
e3 < e1 if (e2-e1) < 0
Hence , if (e2-e1) >= 0 and e3 < e1,
enumFromThenTo e1 e2 e3 will generate the list [e1,e2,..]
(if you dont agree with what I say, then here is the
proof to it :
enumFromThenTo e1 e2 e3 = TakeWhile
(if e2 >= e1 then (<= e3) else (>= e3))
(enumFromThen e1 e2)
e2-e1 >= 0 => e2 >= e1
(if e2 >= e1 then (<=e3) else ....) => (<=e3)
Hence, enumFromThenTo e1 e2 e3 = TakeWhile (<=e3)
(enumFromThen e1 e2)
enumFromThen e1 e2 = [e1,e2..]
Hence ,
enumFromThenTo e1 e2 e3 = TakeWhile (<=e3) [e1,e2..]
As the increment e2-e1 >= 0. Hence [e1,e2..] will have
elements >= e1.
The condition (<=e3) x, where x belongs to [e1,e2..] will
always be true.
Hence TakeWhile (<=e3) [e1,e2..] = [e1,e2..]
Hence enumFromThenTo e1 e2 e3 = [e1,e2..]
( Hope by now you haven't forgotten that e2-e1 >= 0 and e3<e1)
But, actually enumFromThenTo e1 e2 e3 in the above mentioned
condition should be [] !!!
)
Simi. when (e2-e1) < 0 and e3 > e1,
enumFromThenTo e1 e2 e3 = [e1,e2..] and not [].
-----------
Proposed change in defintion of enumFromThenTo,
enumFromThenTo e1 e2 e3
| e2 >= e1 = if (e3 < e1) then []
else TakeWhile (<= e3)
(enumFromThen e1 e2)
| otherwise = if (e3 > e1) then []
else TakeWhile (>= e3)
(enumFromThen e1 e2)
------------
From the description of Airthmetic Sequences (pg. 15)
[1,2..1] = [1,2..] ( here incr. is nonnegative , but e3 is not
less than e1).
Also from the enum definitions [1,2..1] = [1,2..] .
But , I feel it sholud be []. Isn't this more logical ?
-----------
-Namrata
([EMAIL PROTECTED])