[Haskell-cafe] [m..n] question

Simon Richard Clarkstone simon.clarkstone at gmail.com
Fri Sep 26 21:09:06 EDT 2008

Darn, I sent this as personal mail the first time.

Evan Laforge wrote:
>> In Haskell,
>> "The sequence enumFromTo e1 e3 is the list [e1,e1+1,e1+2,...e3].
>>  The list is empty if e1 > e3."
> I like it, since it means that things like [n .. n + length m - 1]
> work as expected when m is [].  Or say 'map (array!) [bsearch x ..
> bsearch y - 1]'.
> Tangent:  Of course, I would prefer the range be half-open, which is a
> pretty consistent standard in the rest of the world.  I've had a
> number of off by one errors from this, and from Array.bounds.  I guess
> it's too late to fix those, though, even if there were agreement that
> they need to be fixed.

It causes problems with types that have an upper bound.  You can't
express Haskell's [False .. True] as a half-open range for example.


One solution would be a new syntax for half-open ranges distinct from
that for closed ranges, or maybe a couple of operators.  Sketching
without a compiler:

lo <. hi = takeWhile (< hi) [lo ..]

data EnumStart a = a :& a

(lo :& mid) <: hi = takeWhile (< hi) [lo, mid ..]

So you can do things like (0 <. 10) or (0 :& 2 <: 10)

Or one could use the same operator for both, if one defined something like:

class OpenEnum bot top where
   (<.) :: bot -> top -> [top]
instance (Enum e) => OpenEnum e e where
   lo <. hi = takeWhile (< hi) [lo ..]
instance (Enum e) => OpenEnum (EnumStart a) a ...
   (lo :& mid) <. hi = takeWhile (< hi) [lo, mid ..]

A better choice of line noise for the operator could still be made though.


More information about the Haskell-Cafe mailing list